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 — 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, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.
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, and the TypeScript Academy course includes 3 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 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; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
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)