Generics for TypeScripts
Generics let functions, classes and types work with many types while keeping type safety, using type parameters like T. Constraints with extends restrict what T can be. TypeScript also ships built-in utility types, such as Partial, Pick, Omit, Record and ReturnType, that transform existing types, saving you from rewriting similar shapes.
Stage: Reusable Types · Part of the TypeScript roadmap · Last reviewed 19 November 2026
What to learn in Generics
- Write a generic function whose return type depends on its argument type
- Constrain a type parameter with extends and keyof
- Create a generic interface for a response wrapper such as ApiResult<T>
- Use Partial, Pick, Omit and Record to derive types from existing ones
- Explain why generics are safer than using any
Generics interview questions
Why use generics instead of any?
With any, the compiler forgets the type, so a function returning any gives callers no help and no checking. A generic function such as identity<T>(x: T): T preserves the relationship between input and output types, keeping autocomplete and error detection.
What do Pick and Omit do?
Pick<T, K> builds a new type containing only the listed keys K of T. Omit<T, K> builds a type with all properties of T except the listed keys. They are useful to derive, for example, a public user type without the password field.
Where Generics fits in the TypeScript roadmap
Generics belongs to the Reusable Types stage (intermediate level), which takes about 17 hours. Write flexible code.
Other units in this stage
Back to the full TypeScript roadmap