You can’t constrain react children like this.
Any react functional component is just a function that has a specific props type and returns JSX.Element
. This means that if you render the component before you pass it a child, then react has no idea what generated that JSX at all, and just passes it along.
And problem is that you render the component with the <MyComponent>
syntax. So after that point, it’s just a generic tree of JSX nodes.
This sounds a little like an XY problem, however. Typically if you need this, there’s a better way to design your api.
Instead, you could make and items
prop on List
which takes an array of objects that will get passed as props to ListItem
inside the List
component.
For example:
function ListItem({ children }: { children: React.ReactNode }) {
return (
<li>{children}</li>
);
}
function List(props: { items: string[] }) {
return (
<ul>
{props.items.map((item) => <ListItem>{item}</ListItem> )}
</ul>
);
}
const good = <List items={['a', 'b', 'c']} />
In this example, you’re just typing props, and List
knows how to generate its own children.