I'm trying to write a component which accepts an array of instances of another component as a prop. That component is MyComponent
, which accepts an array of instances of Title
as one of its arguments. However, I am not able to get TypeScript to type check this:
import * as React from "react";
type TitleProps = {
name: string;
};
function Title(props: TitleProps) {
return null;
}
type MyComponentProps = {
titles: Array<React.ReactElement<TitleProps>>;
}
function MyComponent({ titles }: MyComponentProps) {
return <div>{titles}</div>;
}
function OtherComponent(props: {}) { return null }
const shouldError = <MyComponent titles={[ <div/>, <OtherComponent/> ]} />;
const shouldNotError = <MyComponent titles={[ <Title name="hi"/>, <Title name="hi2"/>, ]} />;
As you can see, I am able to pass whatever I want to the titles
prop, not just instances of <Title/>
.