Say you have a component tree like:
function Parent(props: { componentProp: ReactElement }) {
return (
<>
{props.componentProp}
</>
);
}
function ChildA(props: { message: string }) { return (<h1>{props.message}</h1>) }
function ChildB(props: { message: string }) { return (<h2>{props.message}</h2>) }
function App() {
return (
<Parent componentProp={<ChildA message="hi"/>}
);
}
As written, you could replace ChildA
with ChildB
in componentProp
and Typescript would not complain.
Is it possible to restrict the type of componentProp
such that you could only pass ChildA
, and passing ChildB
would throw a type error?