I am trying to use recently added support for typing children in the TypeScript compiler and @ type / react, but it is struggling. I am using TypeScript version 2.3.4.
Let's say I have this code:
interface TabbedViewProps {children?: Tab[]} export class TabbedView extends React.Component<TabbedViewProps, undefined> { render(): JSX.Element { return <div>TabbedView</div>; } } interface TabProps {name: string} export class Tab extends React.Component<TabProps, undefined> { render(): JSX.Element { return <div>Tab</div> } }
When I try to use these components as follows:
return <TabbedView> <Tab name="Creatures"> <div>Creatures!</div> </Tab> <Tab name="Combat"> <div>Combat!</div> </Tab> </TabbedView>;
I get an error as follows:
ERROR in ./src/typescript/PlayerView.tsx (27,12): error TS2322: Type '{ children: Element[]; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<TabbedView> & Readonly<{ children?: ReactNode; }> ...'. Type '{ children: Element[]; }' is not assignable to type 'Readonly<TabbedViewProps>'. Types of property 'children' are incompatible. Type 'Element[]' is not assignable to type 'Tab[] | undefined'. Type 'Element[]' is not assignable to type 'Tab[]'. Type 'Element' is not assignable to type 'Tab'. Property 'render' is missing in type 'Element'.
It seems to output the type of children as soon as Element[] instead of Tab[] , although this is the only type of children that I use.
EDIT: It would also be nice to restrict the props interface for children, rather than restricting the type of child components directly, since all I need to do is pull some specific details from the child components.
reactjs typescript
Christopher armstrong
source share