Type Safety Across Patterns
You'll learn: How TypeScript handles the three content injection patterns—children, slots, and render props—and which offers the clearest type ergonomics.
The TypeScript Lens
Each pattern has different TypeScript "feel" when you're writing and consuming components:
Children (ReactNode):
The loosest type. ReactNode accepts almost anything—strings, numbers, JSX, arrays, null. Easy to write, but you get minimal IntelliSense and no guarantees about what's actually passed.
interface CardProps {
children: ReactNode;
}
// Consumer gets no help, just passes "whatever"
Slots (Explicit Props): The most explicit. Each slot gets its own typed prop, giving you perfect autocomplete and clear documentation. TypeScript sees what content goes where.
interface CardProps {
header: ReactNode;
footer: ReactNode;
actions?: ReactNode; // Optional slots are obvious
}
// TypeScript shows all available slots in autocomplete
Render Props (Function Signatures): The most informative. Function signatures document what data consumers receive, making the contract crystal clear. You get type-checked arguments and return values.
interface ListProps<T> {
renderItem: (item: T, index: number) => ReactNode;
}
// TypeScript enforces correct function shape and item type
The Tradeoff
Children are frictionless but vague. Slots are verbose but discoverable. Render props are powerful but complex. Your choice depends on whether you value simplicity (children), clarity (slots), or flexibility (render props).
Key Takeaway: TypeScript ergonomics favor slots for simple content injection (explicit props = better autocomplete) and render props for dynamic behavior (function signatures = documented contracts). Children work everywhere but provide the least type information.