Course contentsShow
← React
Lesson 1135 of 2,39629. Composition: SlotsFree lesson

Type Safety for Slot Props

TypeScript patterns for typing slot props as ReactNode, ReactElement, or specific component types.

Type Safety for Slot Props

You'll learn how to use TypeScript to ensure your slot props receive the correct types of content.

The Core Idea

When you accept JSX elements as props (slots), TypeScript needs to know what kind of content is allowed. Without proper typing, you could accidentally pass the wrong type of component or content, leading to runtime errors. TypeScript offers several types specifically designed for this: ReactNode, ReactElement, and specific component types.

Understanding Your Type Options

Think of slot types like size restrictions on boxes: some accept anything (ReactNode), some need specific shapes (ReactElement), and some need exact items (component types).

ReactNode is the most permissive — it accepts anything renderable: strings, numbers, JSX elements, arrays, fragments, or even null. Use this when your slot can display any content.

ReactElement is more strict — it only accepts actual JSX elements (not primitives like strings or numbers). Use this when you need a real component or element.

Specific component types are the most restrictive — they enforce that a particular component is used. This is useful when your layout depends on specific props or behavior.

Type Examples

interface CardProps {
  header: ReactNode;           // Accepts anything renderable
  content: ReactElement;        // Must be JSX element
  footer?: ReactNode;          // Optional, any renderable
}

For enforcing specific components, you can use ReactElement<ComponentProps> to ensure both the element and its props match expectations.

Key Takeaway: Use ReactNode for flexible slots that accept any content, ReactElement when you need actual JSX, and specific component types when your slot requires particular behavior or props — TypeScript will catch mismatches at compile time instead of runtime.