Generics let TypeScript functions, classes, interfaces, and React components work across multiple types while preserving their relationships. A type variable such as T is replaced when the code is used, unlike any, which discards type information. Interviewers use generics to assess reusable API design and type safety. section 3.8 section 3.12
Generic React list
interface ListProps<T> { items: T[]; renderItem: (item: T) => JSX.Element; }\nconst List = <T,>({ items, renderItem }: ListProps<T>) => (\n <ul>{items.map(renderItem)}</ul>\n);\n\n<List<User> items={users} renderItem={renderUser} />\n\nThe component accepts different item types, while renderItem remains tied to the same T. This preserves a useful contract without duplicating the component or weakening it with any. section 3.8
When explaining generics in an interview, connect flexibility to the constraints that keep the API safe:
- Use extends to require capabilities, such as a User shape or a length property, while retaining the specific subtype. section 3.8
- Use keyof to restrict a property key and return the corresponding T[K] type. section 3.8
- Compare generics with any: both permit varied inputs, but generics preserve strict type information throughout the operation. section 3.8
Table 3.6 Key features of functions, classes, and advanced types
|
Topic
|
Fundamentals
|
Applied skills
|
Advanced
|
|
Function types
|
Annotate parameters and return types.
|
Use overloads, optional/default parameters, and rest params effectively.
|
Model polymorphic and higher-order functions with generics and conditional types.
|
|
Classes & interfaces
|
Define classes, implement interfaces, and apply basic access modifiers.
|
Use abstract classes, generics in classes, and modifiers (
public,
private,
protected).
|
Architect type-safe OOP hierarchies with mixins, decorators, and SOLID principles in TypeScript.
|
|
Generics
|
Create generic functions and classes (
<T>).
|
Constrain generics with
extends, use default types, and
infer with conditional types.
|
Architect reusable libraries leveraging advanced generic patterns and utility types.
|
|
Conditional & mapped types
|
Recognize basic mapped types (
Partial<T>,
Readonly<T>).
|
Apply mapped types for object transformations; use
keyof,
in.
|
Build complex utility types and solve deep type transformation problems for scalable systems.
|
|
Utility types
|
Use builtin types like
Partial,
Pick,
Record,
Exclude.
|
Combine utility types for APIs and domain models.
|
Design custom utility types for advanced scenarios like deep partials, recursive types, and API inference.
|
Compare generic fundamentals, constrained generics, default types, conditional inference, and reusable library patterns. section 3.8






















