Skip to article content
FrontendXLesson 32 / 40

Typing React props and children

Go from the first annotated variable to writing your own conditional, mapped and infer-based utility types, while keeping a clear line between compile-time checking and runtime behavior.

intermediate · about 45 minutes · 3 exercises

  • Predict
  • Implement
  • Debug

What you will be able to do

#
  • Type a component props object, including children.
  • Use ReactNode and avoid React.FC pitfalls.
  • Discriminate props for variant components.

Overview

#

A props type

snippet.tsx
type ButtonProps = {
  label: string;
  onClick: () => void;
  disabled?: boolean;
};
function Button({ label, onClick, disabled = false }: ButtonProps) {
  return <button onClick={onClick} disabled={disabled}>{label}</button>;
}

children

snippet.tsx
import type { ReactNode } from "react";
type PanelProps = { title: string; children: ReactNode };

ReactNode covers elements, strings, numbers, arrays and null. ReactElement is narrower.

Why not React.FC

React.FC fixes the return type and, in older @types/react versions, added an implicit children prop (React 19 types no longer do). A plain function with a typed props parameter is clearer and works with generics.

Discriminated props

snippet.tsx
type AlertProps =
  | { variant: "info"; message: string }
  | { variant: "error"; message: string; retry: () => void };

A switch on variant gives the right members in each branch.

Extending native props

snippet.tsx
type InputProps = React.ComponentPropsWithoutRef<"input"> & { label: string };

This reuses the real DOM prop types instead of re-declaring them.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Implement

    Type and implement a `Badge` component with `text: string`, an optional `tone` of `"info" | "warn"`, and optional children.

    starter.typescript
    import type { ReactNode } from "react";
    
    export type BadgeProps = {
      // your code
    };
    
    export function Badge(props: BadgeProps) {
      // your code
    }
    

    Reveal the reference answer
    reference.typescript
    import type { ReactNode } from "react";
    
    export type BadgeProps = {
      text: string;
      tone?: "info" | "warn";
      children?: ReactNode;
    };
    
    export function Badge({ text, tone = "info", children }: BadgeProps) {
      return <span data-tone={tone}>{text}{children}</span>;
    }
    

    Authored expected output: Compiles with no errors. A call with tone="bad" fails to compile.

    The literal union restricts tone and ReactNode accepts any renderable child.

  2. Exercise 2Predict

    In `type P = { children: ReactNode }`, what is the type of `children` when the element is `<P>hi</P>`?

    Choose the best answer
  3. Exercise 3Debug

    This fails because the discriminated union is not narrowed. Fix the component body. ```tsx type Props = { kind: "a"; a: number } | { kind: "b"; b: string }; function C(props: Props) { return <div>{props.a ?? props.b}</div>; } ```

    starter.typescript
    type Props = { kind: "a"; a: number } | { kind: "b"; b: string };
    function C(props: Props) { return <div>{props.a ?? props.b}</div>; }
    

    Reveal the reference answer
    reference.typescript
    type Props = { kind: "a"; a: number } | { kind: "b"; b: string };
    function C(props: Props) {
      return <div>{props.kind === "a" ? props.a : props.b}</div>;
    }
    

    Authored expected output: Compile error resolved: Property 'a' does not exist on type '{ kind: "b"; b: string }'. Narrowing on props.kind fixes it.

    The discriminant kind must be checked before the member is read.

Common misconceptions

#
  • React.FC is required for typed components.
  • children is always a ReactElement.
  • Prop types are checked at runtime.

Follow-up practice

#
  • typescript-react-hooks

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.