Generics
Generic Constraints — extends
Master Generic Constraints using the extends keyword, keyof constraints, restricting generic types, and preventing invalid arguments in TypeScript.
Last Updated: July 29, 2026
•
10 min read
By default, a generic parameter can accept any type whatsoever (strings, numbers, functions, null, objects). Generic Constraints allow you to restrict the allowed types by forcing to extend a specific interface or type using the extends keyword.
1. Syntax for Generic Constraints
2. Using keyof in Generic Constraints
A very common TypeScript pattern is constraining a second generic parameter to be a valid key of the first object parameter using :
3. Constraining Objects with Interfaces
4. Interactive Code Playground
Test generic constraints below:
5. Summary Table
| Constraint Pattern | Syntax Example | Meaning |
|---|---|---|
| Interface Constraint | | T must contain all properties defined in HasId |
keyof Constraint | | K must be a valid key name of object type T |
| T must be assignable to string |