TypeScript in real projects · core
Typing React components and hooks
Type props as an object type, children as React.ReactNode, events with React's event types, refs with useRef<HTMLDivElement>(null), and make reusable components generic when their data type varies.
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:
type Props = { title: string; onSelect?: (id: string) => void; children?: React.ReactNode }. Avoid React.FC for new code; type the props parameter. - Events:
React.ChangeEvent<HTMLInputElement>,React.MouseEvent<HTMLButtonElement>; or type the handlerReact.ChangeEventHandler<HTMLInputElement>. - useState with null start:
useState<User | null>(null). useRef for DOM:useRef<HTMLInputElement>(null). In React 19, ref is a normal prop:ref?: React.Ref<HTMLButtonElement>. - Extend native props:
React.ComponentProps<'button'> & { variant: 'primary' | 'ghost' }. - Generic component:
function List<T>({ items, render }: { items: T[]; render: (x: T) => React.ReactNode }). - Custom hook returning a tuple: add
as constso it isn't widened to an array of unions.
The red pen
Common traps: mistake → fix
Mistake: "React.FC is required"
Fix: it is optional; plain functions with typed props are the common style now.
Mistake: "forwardRef is needed for refs"
Fix: React 19 passes ref as a prop to function components.
Easy to hard
Questions interviewers ask, easy to hard
- How do you type a Button that accepts all native button props?
- How do you type a generic Table or List component?
- How do you type a custom hook's return value?
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.