Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Generics

Generic constraints with extends

Reference for TypeScript generic constraints: limit type parameters with extends, access constrained members, combine constraints and read the common errors.

A constraint limits which types a type parameter accepts. You write it with extends: <T extends Shape> means "any type assignable to Shape". Inside the function or class the compiler then knows that T has at least the members of Shape, so you can use them safely.

Without a constraint, T is treated as unknown: you cannot read a property from it. Constraints are how you say "any type, as long as it has a length" or "any object with an id" while still returning the exact, more specific type the caller passed in.

TypeScript
function longest<T extends { length: number }>(a: T, b: T): T {
  return a.length >= b.length ? a : b;
}

Try it

TypeScript Keeping the specific type

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Object constraints, unions and class constraints

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

TypeScript
function f<T extends Constraint>(x: T): T
function g<T extends A & B, U extends keyof T>(x: T, key: U): T[U]
function h<T extends new (...args: any[]) => object>(ctor: T): InstanceType<T>
Part Description
T extends C T must be assignable to C. C can be an object type, union, class, function type, tuple or another type parameter.
A & B An intersection constraint: the argument must satisfy both.
U extends keyof T A constraint that depends on an earlier parameter (see Constraints with keyof).

Errors: passing a type that does not satisfy the constraint is TS2345 (argument) or TS2344 ("Type X does not satisfy the constraint Y") for explicit type arguments.

Usage notes

  • extends here means "is assignable to", not class inheritance. T extends string accepts "a" and string but not number.
  • Inside the body, T is only known as its constraint. Returning { id: 0 } where T extends HasId is an error, because the caller's T might have more properties. Cast carefully or restructure.
  • Prefer the narrowest useful constraint. T extends object is weaker than T extends { id: number }; the second gives access to id.
  • Self-referencing constraints are allowed: T extends Comparable<T> describes types that can compare themselves to their own kind.
  • Defaults and constraints combine: <T extends object = {}>. The default must satisfy the constraint.
  • Do not constrain when unknown is enough. If the function only passes values through, no constraint is needed.
  • Constraint is not a runtime check. The compiled JavaScript contains no validation; untrusted data still needs narrowing or a schema.
  • Constraint to any[] is the usual way to capture "some parameter list" or tuple; use readonly unknown[] when the function does not mutate it.