0Pricing
TypeScript Academy · Lesson

Generic Constraints with extends

Restrict type parameters to specific shapes.

Generic Constraints with extends is a free TypeScript Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Welcome

Generic constraints narrow what types can be passed as type arguments. Use `extends` to require that T has certain properties or matches a certain shape.

The Problem Without Constraints

Without constraints, TypeScript doesn't know what operations are valid on T, so it only allows operations common to all types.
function getLength<T>(val: T): number {
  // return val.length; // Error — T might not have .length
}

Adding a Constraint

Use `T extends Shape` to require that T has at least the properties of Shape.
function getLength<T extends { length: number }>(val: T): number {
  return val.length;
}
getLength('hello');  // OK — string has .length
getLength([1, 2]);   // OK — array has .length
// getLength(42);   // Error — number has no .length

Constraining to an Interface

Constrain T to implement a specific interface.
interface Named { name: string; }
function greet<T extends Named>(person: T): string {
  return 'Hello, ' + person.name;
}

keyof Constraint

Use `K extends keyof T` to constrain K to the keys of T. This enables safe property access.
function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] {
  return obj[key];
}
const age = getProperty({ name: 'Alice', age: 30 }, 'age'); // number

Extending a Union

T can be constrained to a union type, restricting the type argument to specific values.
type Status = 'active' | 'inactive' | 'banned';
function setStatus<T extends Status>(status: T): void {
  console.log('Setting status:', status);
}

Constraining to a Constructor

Constrain T to be a class (have a constructor) so you can create instances inside the function.
function create<T>(ctor: new () => T): T {
  return new ctor();
}

Multiple Constraints with Intersection

Constrain T to satisfy multiple interfaces using an intersection.
interface Serializable { serialize(): string; }
interface Loggable { log(): void; }
function process<T extends Serializable & Loggable>(item: T): void {
  item.log();
  console.log(item.serialize());
}

Constraint Inheritance

Constraints work with inheritance. If T extends a base class, you can call all base class methods.
class Animal { breathe() { return 'breathing'; } }
function care<T extends Animal>(a: T): string { return a.breathe(); }

T extends Itself (Recursive)

Constraints can be self-referential for recursive data structures.
interface Comparable<T> { compareTo(other: T): number; }
function max<T extends Comparable<T>>(a: T, b: T): T {
  return a.compareTo(b) >= 0 ? a : b;
}

Constraint Error Messages

When a type argument doesn't satisfy a constraint, TypeScript reports a clear error naming both the argument and the constraint.

Quick Check

What does `K extends keyof T` constrain K to?

Recap

Use `T extends SomeType` to constrain generic type parameters. This allows you to call specific methods on T, use keyof for safe property access, and build type-safe utilities.

Frequently asked questions

Is the “Generic Constraints with extends” lesson free?

Yes — the full text of “Generic Constraints with extends” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Generic Constraints with extends”?

Restrict type parameters to specific shapes. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Generic Constraints with extends” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Generic Functions: Type Parameters
  2. Generic Interfaces and Type Aliases
  3. Generic Constraints with extends
  4. Default Type Parameters
← Back to TypeScript Academy