0Pricing
TypeScript Academy · Lesson

Generic Constraints: Narrowing Type Parameters

Use extends to enforce shape requirements on generics.

Welcome

Advanced generic constraints let you narrow what types can be used as type arguments, enabling type-safe operations on complex shapes.

Constraint Recap

T extends U restricts T to types that are subtypes of U. This gives TypeScript knowledge about T's shape inside the function.
function len<T extends { length: number }>(val: T): number {
  return val.length;
}

All lessons in this course

  1. keyof and Indexed Access Types
  2. Generic Constraints: Narrowing Type Parameters
  3. Conditional Types: T extends U ? X : Y
  4. Distributive Conditional Types
← Back to TypeScript Academy