Common Children Typing Pitfalls
You'll learn: How to avoid the most frequent TypeScript errors when typing children props, especially around string vs ReactElement mismatches and inference issues.
The Problem: Children Type Mismatches
TypeScript's complaints about children often stem from overly-restrictive types meeting flexible reality. The classic mistake: declaring a prop that accepts only ReactElement, then passing a string child.
interface ButtonProps {
children: ReactElement; // ❌ Too strict!
}
function Button({ children }: ButtonProps) {
return <button>{children}</button>;
}
// TypeScript error: Type 'string' is not assignable to type 'ReactElement'
<Button>Click me</Button>
Why it fails: "Click me" is a string, not a ReactElement. TypeScript enforces this strictly.
Common Pitfalls
1. ReactElement When You Mean ReactNode
If users might pass text, numbers, or fragments, use ReactNode (the broadest type) instead:
interface ButtonProps {
children: ReactNode; // ✅ Accepts strings, numbers, elements, etc.
}
2. Forgetting Children Are Optional by Default
Without PropsWithChildren or explicit annotation, TypeScript won't infer children exists:
// No children type declared
function Wrapper(props: {}) {
return <div>{props.children}</div>; // ❌ Property 'children' does not exist
}
3. Array Type Confusion
ReactElement[] requires every child to be an element. Mixed content breaks:
children: ReactElement[]; // ❌ Fails with: <div>Text {element}</div>
children: ReactNode; // ✅ Handles mixed content
Think of it like luggage restrictions: ReactElement is "carry-on bags only," while ReactNode is "any baggage accepted."
The Inference Trap
TypeScript doesn't automatically infer children from JSX usage—you must declare the type explicitly in props. Relying on inference causes silent failures.
Key Takeaway: Use ReactNode for flexible children; reserve ReactElement only when you truly need JSX-only constraints and will validate accordingly.