- React
- Type-Safe React: Components, Generics, and Hooks
React TypeScript Interview Questions
React TypeScript interview questions matter because TypeScript is mandatory across modern FAANG frontend engineering teams. Interviewers expect candidates to write strictly typed components, avoid any, and use advanced generic constraints.
Master the distinction between ReactNode and ReactElement, accurately type DOM refs versus mutable containers, build generic table and dropdown components that infer data row types, and type polymorphic components that dynamically validate HTML attributes.
The lessons build from the types every React codebase needs, props, children and event handlers, through typing useState, useReducer and refs to DOM elements, to the two patterns senior interviews use to separate candidates: generic components such as a type-safe table or select, and polymorphic components that take an as prop and still type the props of the element they render.
Interviewers ask these to see whether you can make the compiler catch real bugs rather than silencing errors with any.
React TypeScript Interview Questions: 4 lessons
Typing Components, Props, Children & Events
Typing Components, Props, Children & Events: in modern React TypeScript, type components as standard functions with typed prop interfaces rather than React.FC. Type children with React.ReactNode, and use specific synthetic event types like React.MouseEvent<HTMLButtonElement> or React.ChangeEvent<HTMLInputElement> for full type safety.
Typing Hooks & Refs: State, Reducers & DOM
Typing Hooks & Refs: TypeScript can infer simple hook states, but union states require explicit generics like useState<User | null>(null). Type DOM refs with useRef<HTMLDivElement>(null) for read-only element access, while mutable container refs use explicit non-null initial values. Type useReducer with strict discriminated action unions.
Generic Components: Type-Safe Tables & Selects
Generic React components accept TypeScript type parameters `<T>`, enabling reusable UI primitives like Tables, Selects, and Virtual Lists to enforce full type safety on data items, callbacks, and custom cell renderers without losing type inference.
Advanced TypeScript: Typing Polymorphic as Props
Typing polymorphic components requires advanced TypeScript generics that map the "as" prop to React.ElementType, combine custom props with React.ComponentPropsWithoutRef<E>, and omit prop collisions to ensure accurate HTML attribute validation and ref forwarding.
Frequently asked questions
How do you type children in a React component with TypeScript?
Use React.ReactNode for anything React can render: elements, strings, numbers, arrays and null. Use React.ReactElement only when you need an actual element, and PropsWithChildren<P> to add children to an existing props type.
How do you type an onChange event in React with TypeScript?
Use the event type for the element: React.ChangeEvent<HTMLInputElement> for an input or React.ChangeEvent<HTMLSelectElement> for a select. event.target is then typed, so event.target.value is a string without a cast.
What is a polymorphic component in React?
A component that renders a different element or component depending on an as prop, such as a Button that can render an a or a router Link, while TypeScript checks that the props you pass are valid for that element. It is typed with a generic over the element type and React.ComponentPropsWithoutRef.
