Skip to article content
FrontendXLesson 40 / 40

Typing React Components in TypeScript

Build, explain and debug React 19 interfaces: components, rendering, state, effects, identity, forms and actions, server components, concurrency, performance and testing, ending with architecture and typing work on real component challenges.

intermediate · about 55 minutes · 4 exercises

  • Predict
  • Explain
  • Implement
  • Debug

What you will be able to do

#
  • Type props, children and event handlers precisely.
  • Type refs and generic components.
  • Avoid any and unnecessary casts in component code.

Overview

#

Typing props

Define a props type for every component. Use a type alias or interface and destructure in the parameter list. Avoid React.FC; type the props directly so children is explicit.

snippet.tsx
type ButtonProps = {
  label: string;
  onClick: () => void;
  variant?: 'primary' | 'secondary';
};

function Button({ label, onClick, variant = 'primary' }: ButtonProps) {
  return <button data-variant={variant} onClick={onClick}>{label}</button>;
}

Children

Type children as React.ReactNode, which covers elements, strings, numbers, fragments and null. Use React.ReactElement only when you require a single element.

snippet.tsx
type CardProps = { children: React.ReactNode };

Event handlers

Type handlers with the specific event type. Use React.ChangeEvent<HTMLInputElement> for input changes and React.MouseEvent<HTMLButtonElement> for clicks. If the handler only reads e.target.value, you can type the parameter inline and let TypeScript infer it.

snippet.tsx
function onChange(e: React.ChangeEvent<HTMLInputElement>) {
  setValue(e.target.value);
}

Refs

A ref to a DOM element is typed by the element: useRef<HTMLInputElement>(null) for an input. In React 19 ref is a normal prop, so type it as React.Ref<T> and pass it through.

snippet.tsx
function Input({ ref }: { ref: React.Ref<HTMLInputElement> }) {
  return <input ref={ref} />;
}

Generic components

A generic component keeps the relationship between its props. Type the type parameter on the function so callers get inference.

snippet.tsx
function List<T>({ items, render }: { items: T[]; render: (item: T) => React.ReactNode }) {
  return <ul>{items.map((item, i) => <li key={i}>{render(item)}</li>)}</ul>;
}

Pitfall: any and casts

Using any or as to silence the compiler hides real errors. Type the prop, narrow with a discriminant, or use a type guard instead of casting.

Pitfall: typing children too narrowly

Typing children as a string breaks as soon as a caller passes an element. Use React.ReactNode unless you truly require one element.

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 1Predict

    Does this type error? Why?

    Read the code, then choose the best answer. The reference explanation stays hidden until you respond.
    stimulus.tsx
    type Props = { children: string };
    function Wrap({ children }: Props) { return <div>{children}</div>; }
    <Wrap><span>hi</span></Wrap>;
    Choose the best answer
  2. Exercise 2Implement

    Type a generic List component that infers the item type from its props.

    starter.tsx
    export function List({ items, render }) {
      return <ul>{items.map((item, i) => <li key={i}>{render(item)}</li>)}</ul>;
    }

    Reveal the reference answer
    reference.tsx
    export function List<T>({ items, render }: { items: T[]; render: (item: T) => React.ReactNode }) {
      return <ul>{items.map((item, i) => <li key={i}>{render(item)}</li>)}</ul>;
    }

    Authored expected output: Callers get the item type inferred from the items array.

    The type parameter T ties items and render together, so passing an array of a type makes render receive that same type. Callers get inference without specifying the type argument.

  3. Exercise 3Debug

    This handler uses any and hides a bug. Explain and fix.

    starter.tsx
    function onChange(e: any) {
      setValue(e.target.value);
    }
    <input onChange={onChange} />

    Reveal the reference answer
    reference.tsx
    function onChange(e: React.ChangeEvent<HTMLInputElement>) {
      setValue(e.target.value);
    }
    <input onChange={onChange} />

    Authored expected output: The handler is typed, so accessing a wrong property is a compile error.

    any disables checking, so a typo like e.target.valuee would not be caught. Typing the event with the input change event makes target.value known and checked.

  4. Exercise 4Explain

    Explain how to type a component's props, children, events and refs, and why any is discouraged.

    starter.tsx
    // props, children, events, refs

    Reveal the reference answer
    reference.tsx
    Type props with a dedicated type alias and destructure them, avoiding React.FC so children is explicit. Type children as React.ReactNode to accept elements, strings and fragments. Type event handlers with the specific event generic, such as React.ChangeEvent<HTMLInputElement>, so properties like target.value are known. Type refs by their element, useRef<HTMLInputElement>(null), and in React 19 type a passed ref as React.Ref<T>. any is discouraged because it disables checking, so mistakes that the type system would catch, such as a misspelled property or a wrong event type, reach runtime. Prefer a precise type, a union or a type guard over any or a cast.

    Authored expected output: A written guide to typing each part plus the reason to avoid any.

    The answer should cover the four typing areas and connect any to lost compile-time safety.

Common misconceptions

#
  • React.FC is required. Type the props directly; React.FC adds implicit children and other drawbacks.
  • children should be typed as string. Use React.ReactNode to accept elements and fragments.
  • as is fine to fix type errors. Casts hide errors; narrow or type the value instead.
  • Event handlers can take any. Type them with the specific event generic for safety.

Follow-up practice

#
  • react-custom-hooks
  • react-component-architecture

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.