Generic functions & constraints (extends, default params)
Create generic functions, add constraints with extends, and use default type parameters for ergonomic APIs.
Generic functions & constraints (extends, default params) is a free TypeScript Academy lesson on CoddyKit. This is lesson 1 of 3. 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, and your progress syncs across the web and the CoddyKit app. The TypeScript Academy course includes 3 lessons in total.
Intro
Goal: Make functions reusable and type-safe with generics. You will define type parameters, add extends constraints, and set default type params for better DX.
Identity generic
Define a type parameter <T>. The compiler infers T from arguments, so callers rarely annotate it.
function identity<T>(x: T): T {
return x;
}
const a = identity(42); // T inferred as number
const b = identity("ts"); // T inferred as string
console.log(a, b);Constraint with extends
Use extends to require structure (e.g., an id). T can include more fields thanks to structural typing.
function getId<T extends { id: number }>(item: T): number {
return item.id;
}
getId({ id: 1, name: "Ada" }); // OK (has id)
// getId({ name: "Lin" }); // Error: missing idKeyof constraint
Constrain K with keyof T to only allow property names from T. The return type is the property's type T[K].
function pluck<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
const user = { id: 7, name: "Neo" };
const v1 = pluck(user, "name"); // string
const v2 = pluck(user, "id"); // number
console.log(v1, v2);Default type params
Default type params reduce boilerplate. Callers can omit <T> for common cases but still override when needed.
interface Box<T = string> {
value: T;
}
const aBox: Box = { value: "hi" }; // T defaults to string
const nBox: Box<number> = { value: 123 }; // explicit override
console.log(aBox.value, nBox.value);Tips
Tips:
- Let inference do the work; avoid redundant T annotations.
- Add extends only when the implementation needs members of T.
- Provide defaults for ergonomic, common cases.
Constraint check
Quick check: What does T extends { id: number } ensure?
Recap
Recap: Define generic functions, constrain with extends, and set default type parameters to keep APIs flexible yet friendly.
Learn TypeScript with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 101
- Lessons
- 352
Frequently Asked Questions
Is the “Generic functions & constraints (extends, default params)” lesson free?
Yes — the full text of “Generic functions & constraints (extends, default params)” is free to read here on the web. 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. The TypeScript Academy course includes 3 lessons in total.
What will I learn in “Generic functions & constraints (extends, default params)”?
Create generic functions, add constraints with extends, and use default type parameters for ergonomic APIs. 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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 3.
How long does the “Generic functions & constraints (extends, default params)” 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 & constraints (extends, default params)
- Partial, Readonly, Record, Pick, Omit
- keyof/indexed access types (intro)