Constrain with extends: <T extends { id: string }> so only objects with id are allowed.
Multiple constraints
<T extends Identifiable & Serializable> intersects requirements. Use keyof constraints for safe property access: <T, K extends keyof T>.
Self-check
- Constrain a generic to
number[]and return the max element.
Constraints document minimum capabilities a type parameter must provide—similar to Java bounds, but checked at compile time only.
When a constraint is too loose, errors surface deep inside the function body; tighten the bound so misuse fails at the call site.
Practice: Apply generic-constraints in the playground, then explain generic constraints in one sentence without looking at notes.