Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Concepts and Comparisons

TypeScript with React

How to type React components in TypeScript: props, children, state, events, refs, reducers and generic components, with runnable stand-ins for the React types.

React components are functions, so typing them is mostly typing functions: an object type for props, a return type that React can render, and callbacks whose parameter types come from React's event types. Everything else, such as state, reducers and context, is ordinary TypeScript applied to a specific API.

React itself is not available in the runnable examples on this page, because the single-file runner has no npm packages. The real component code is shown read-only, and the runnable examples use small hand-written stand-ins to demonstrate the same typing ideas.

TSX
import { useState, type ReactNode } from "react";

interface ButtonProps {
  label: string;
  onPress: () => void;
  children?: ReactNode;
}

export function Button({ label, onPress, children }: ButtonProps) {
  const [presses, setPresses] = useState(0);
  return (
    <button onClick={() => { setPresses(presses + 1); onPress(); }}>
      {label} ({presses}) {children}
    </button>
  );
}

Try it

TypeScript Props as an object type, state as a generic

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Reducers with discriminated unions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Common types

Need Type
Anything React can render ReactNode
A single element ReactElement or JSX.Element
Children prop children?: ReactNode, or PropsWithChildren<P>
Click handler MouseEvent<HTMLButtonElement> (or just write the handler inline and let context type it)
Input change ChangeEvent<HTMLInputElement>
Form submit FormEvent<HTMLFormElement>
Ref to a DOM node useRef<HTMLInputElement>(null)
Props of a native element ComponentProps<"button">
State that can be empty useState<User | null>(null)
Context createContext<Value | null>(null) plus a hook that throws when it is null

Patterns

  • Do not use React.FC for plain components. Annotating the props parameter is clearer and avoids implicit children confusion in older type versions.
  • Extend native props with interface Props extends ComponentProps<"input"> { label: string } so wrappers forward everything.
  • Discriminated union props make impossible combinations impossible: { kind: "link"; href: string } | { kind: "button"; onClick: () => void }.
  • Generic components are generic functions: function List<T>(props: { items: T[]; render: (item: T) => ReactNode }). Write <List items={users} ... /> and T is inferred.
  • Let inline callbacks infer. onChange={(e) => ...} already knows e from the prop's type; annotate only handlers defined elsewhere.
  • Type useReducer with the reducer function, as in the second example, and its dispatch type follows.

Usage notes

  • tsconfig: set jsx to react-jsx (React 17 and later), plus strict; the @types/react package supplies the types in React 18 and earlier setups, and in React 19 as well.
  • Server data is unknown until validated; see Runtime validation.
  • as casts on events hide mistakes. Prefer e.currentTarget (typed by the element) to e.target (typed as EventTarget).
  • Optional props read as T | undefined; give them defaults in destructuring.
  • Constrain generics (T extends { id: string }) when the component needs to read a field.