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.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Props:
function Button({ variant = "primary", ...rest }: ButtonProps); default values with default parameters (functiondefaultPropswere removed in React 19). - Reuse native props:
type ButtonProps = ComponentPropsWithoutRef<"button"> & { variant?: "primary" | "ghost" }. In React 19refis a regular prop, soComponentProps<"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:
useRefrequires an argument; ref callbacks must not implicitly return; globalJSXnamespace removed (useReact.JSX). - Validate API data at runtime (zod) at the boundary; types alone do not check JSON.
The red pen
Common traps: mistake → fix
Mistake: "
anyfor 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
- Type a
Buttonthat renders<a>when givenhrefand<button>when givenonClick, but never both. - Type a generic
<Table<T>>whose column keys must be keys of T. - How do you extend native input props?
- How do you type loading/error/success state?
- 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 voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.