How do I restrict the type of React Children in TypeScript, using the newly added support in TypeScript 2.3?

Edit 2: Turns out that this approach prevent the warning, but according to the comments TabProps aren’t properly checked.

You should try to set children of interface TabbedViewProps like so

interface TabbedViewProps { children?: React.ReactElement<TabProps>[] }

The idea here is not to tell your TabbedView has an array of Tab, but instead tell your TabbedView he has an array of element which takes specific props. In your case TabProps.

Edit ( thx to Matei ):

interface TabbedViewProps {
    children?: React.ReactElement<TabProps>[] | React.ReactElement<TabProps>
}

Leave a Comment