Type Parameter Syntax
You'll learn: The angle bracket notation (<>) that declares type parameters in TypeScript function signatures.
The Core Idea
When you create a generic function, you need to tell TypeScript "this function has a type variable." You do this using angle brackets (<>) placed immediately after the function name and before the parameter list.
Think of it like declaring a variable in math: before you write f(x) = x + 1, you need to establish that x is your variable. Similarly, before TypeScript can use a type parameter like T, you must declare it between angle brackets.
The Syntax Pattern
Here's the structure:
function functionName<TypeParameter>(param: TypeParameter): TypeParameter {
// function body
}
The <TypeParameter> part is where you declare your type parameter. It sits between the function name and the parentheses.
Real Examples
function identity<T>(value: T): T {
return value;
}
function first<Item>(array: Item[]): Item {
return array[0];
}
function pair<A, B>(a: A, b: B): [A, B] {
return [a, b];
}
Notice how <T>, <Item>, and <A, B> come right after the function name? That's the type parameter declaration zone. Once declared, those type parameters can be used anywhere in that function's signature and body.
Key Takeaway: Type parameters are declared using angle brackets (<>) immediately after the function name, creating type variables you can use throughout the function signature.