Skip to the page

Forms, TypeScript and security · senior

TypeScript for React components

Type props as plain object types, reuse native element props with ComponentProps, model "either this or that" props with discriminated unions, and use generics for reusable components like tables and selects.

The notes

What you must know

  • Props: function Button({ variant = "primary", ...rest }: ButtonProps); default values with default parameters (function defaultProps were removed in React 19).
  • Reuse native props: type ButtonProps = ComponentPropsWithoutRef<"button"> & { variant?: "primary" | "ghost" }. In React 19 ref is a regular prop, so ComponentProps<"button"> includes it.
  • Children: children?: React.ReactNode. Events: React.ChangeEvent<HTMLInputElement>, React.MouseEvent<HTMLButtonElement>.
  • Mutually exclusive props with a discriminated union: { as: "link"; href: string } | { as: "button"; onClick: () => void }, using a string literal tag (not a boolean) so TypeScript narrows each branch.
  • Generic components: function Table<T>({ rows, columns }: { rows: T[]; columns: { key: keyof T; header: string }[] }); for selects, onChange(value: T).
  • Async state as a union: { status: "loading" } | { status: "error"; error: string } | { status: "success"; data: User[] } instead of three booleans.
  • React 19 typing changes: useRef requires an argument; ref callbacks must not implicitly return; global JSX namespace removed (use React.JSX).
  • Validate API data at runtime (zod) at the boundary; types alone do not check JSON.

The red pen

Common traps: mistake → fix

  • Mistake: "any for event handlers is fine"

    Fix: you lose autocompletion and safety; use the React event types.

  • Mistake: "A boolean tag (isLink) is a fine discriminant"

    Fix: a string literal union scales and narrows clearly.

  • Mistake: "Types guarantee API data shape"

    Fix: only runtime validation does.

Easy to hard

Questions interviewers ask, easy to hard

  1. Type a Button that renders <a> when given href and <button> when given onClick, but never both.
  2. Type a generic <Table<T>> whose column keys must be keys of T.
  3. How do you extend native input props?
  4. How do you type loading/error/success state?
  5. What changed in React 19 types?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview