Generic Functions: Type Parameters
Add type parameters to functions for reusability.
Generic Functions: Type Parameters is a free TypeScript Academy lesson on CoddyKit — lesson 1 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 Generics Solve
// Without generics — loses type info
function first(arr: any[]): any { return arr[0]; }
// With generics — type-safe
function first<T>(arr: T[]): T { return arr[0]; }Type Parameter Syntax
function identity<T>(value: T): T {
return value;
}
const n = identity(42); // T is number
const s = identity('hello'); // T is stringType Inference for Generic Functions
function wrap<T>(val: T): T[] { return [val]; }
const nums = wrap(42); // T inferred as number -> number[]
const strs = wrap('hi'); // T inferred as string -> string[]Multiple Type Parameters
function pair<A, B>(a: A, b: B): [A, B] {
return [a, b];
}
const p = pair(1, 'hello'); // [number, string]Generic Array Functions
function filterNull<T>(arr: (T | null)[]): T[] {
return arr.filter((x): x is T => x !== null);
}Explicit Type Arguments
function create<T>(): T | null { return null; }
const user = create<User>(); // T cannot be inferred — must be explicitGeneric vs Any
function echo<T>(val: T): T { return val; }
const n: number = echo(42); // OK
// const s: string = echo(42); // Error — T is numberGeneric Arrow Functions
const identity = <T>(val: T): T => val;
// In .tsx files:
const identity2 = <T,>(val: T): T => val;Returning a Different Type
function mapArray<T, U>(arr: T[], fn: (item: T) => U): U[] {
return arr.map(fn);
}
const lengths = mapArray(['hello', 'world'], s => s.length);
// lengths: number[]Generic Parameters with Defaults
function createList<T = string>(): T[] { return []; }
const list = createList(); // T defaults to stringQuick Check
Recap
Frequently asked questions
Is the “Generic Functions: Type Parameters” lesson free?
Yes — the full text of “Generic Functions: Type Parameters” 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 Functions: Type Parameters”?
Add type parameters to functions for reusability. 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 4, so you can start here or from the beginning and move at your own pace.
How long does the “Generic Functions: Type Parameters” 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