Skip to content

[@types/react] return array without fragments #26890

@vanyathecyborg

Description

@vanyathecyborg

I've had issues typing components that return array without Fragments like this:

Type '({ slides }: Props & { children?: ReactNode; }) => Element[]' is not assignable to type 'StatelessComponent<Props>'.
Type 'Element[]' is not assignable to type 'ReactElement<any>'.
        Property 'type' is missing in type 'Element[]'.
type SlideItem = {
    id: number,
    url: string,
    type: string,
    title: string,
}

interface Props {
    slides: Array<SlideItem>
}

const Slide : React.SFC<Props> = ({ slides }) => (
    slides.map(item => (
        <Image
            key={item.id}
            url={item.url}
        >
            <StyledSlide>
                <Heading
                    uppercase
                    mono
                    size="M"
                >
                    {item.type}
                </Heading>
                <Heading
                    uppercase
                    mono
                    size="XL"
                    marginTop={25}
                >
                    {item.title}
                </Heading>
            </StyledSlide>
        </Image>
    ))
);

If I wrap this in a Fragment, then it works:

<Fragment>
		{slides.map(item => (
                    ...
		))}
	</Fragment>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions