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
The Problem Without Constraints
function getLength<T>(val: T): number {
// return val.length; // Error — T might not have .length
}Adding a Constraint
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 .lengthConstraining to an Interface
interface Named { name: string; }
function greet<T extends Named>(person: T): string {
return 'Hello, ' + person.name;
}keyof Constraint
function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
const age = getProperty({ name: 'Alice', age: 30 }, 'age'); // numberExtending a Union
type Status = 'active' | 'inactive' | 'banned';
function setStatus<T extends Status>(status: T): void {
console.log('Setting status:', status);
}Constraining to a Constructor
function create<T>(ctor: new () => T): T {
return new ctor();
}Multiple Constraints with 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
class Animal { breathe() { return 'breathing'; } }
function care<T extends Animal>(a: T): string { return a.breathe(); }T extends Itself (Recursive)
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
Quick Check
Recap
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
- Generic Functions: Type Parameters
- Generic Interfaces and Type Aliases
- Generic Constraints with extends
- Default Type Parameters