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.
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
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.
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.FCfor plain components. Annotating the props parameter is clearer and avoids implicitchildrenconfusion 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} ... />andTis inferred. - Let inline callbacks infer.
onChange={(e) => ...}already knowsefrom the prop's type; annotate only handlers defined elsewhere. - Type
useReducerwith the reducer function, as in the second example, and its dispatch type follows.
Usage notes
tsconfig: set jsx toreact-jsx(React 17 and later), plusstrict; the@types/reactpackage supplies the types in React 18 and earlier setups, and in React 19 as well.- Server data is
unknownuntil validated; see Runtime validation. ascasts on events hide mistakes. Prefere.currentTarget(typed by the element) toe.target(typed asEventTarget).- 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.