Generics
Your first T
A type parameter is a placeholder that the caller fills in with a real type. The name T is tradition for a first placeholder. One generic function below replaces a whole family of single type copies.
function first<T>(items: T[]): T | undefined { return items[0]; } console.log(first([7, 8]));
7
Let TS infer T
You rarely write the type argument because TypeScript reads it from the value you pass. Passing strings makes T string with no extra markup. Inference keeps generic calls as short as plain calls.
function first<T>(items: T[]): T | undefined { return items[0]; } const n: number | undefined = first([1, 2, 3]); const s: string | undefined = first(["a", "b"]); console.log(n); console.log(s);
1
a
Explicit type args
Sometimes inference picks a wider type than you want, or there is no argument to read from. Angle brackets after the name pin the type down by hand. Explicit arguments also document intent for the next reader.
function first<T>(items: T[]): T | undefined { return items[0]; } const xs: number[] = []; const f: number | undefined = first<number>(xs); console.log(f);
undefined
Generic array helpers
Library helpers like map and filter are generic functions built into every array. The callback return type becomes the new element type automatically. Chaining them transforms lists without a single loop in sight.
const nums: number[] = [1, 2, 3]; const doubled: number[] = nums.map((n) => n * 2); console.log(doubled.join(", "));
2, 4, 6
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What is a type parameter such as
Tinfunction first<T>(items: T[])?Answer
A placeholder that the caller fills with a real type
The parameter is a type, chosen by the call or inferred from the argument, not a run-time value.
-
How does the compiler pick
Tin the callfirst([1, 2, 3])?Answer
It infers
numberfrom the array argumentThe argument says the items are numbers, so the inferred type argument is
number. -
When is an explicit type argument such as
first<number>(xs)useful?Answer
When there is nothing to infer from, or inference picks a wider type
An empty array or a deliberately narrow type are the cases where the angle brackets help.
-
What does this program print?
function first<T>(items: T[]): T | undefined { return items[0]; } console.log(first([7, 8]));Answer
7
Index zero holds 7, and the function returns it.
-
What does this program print?
function first<T>(items: T[]): T | undefined { return items[0]; } const xs: number[] = []; const f: number | undefined = first<number>(xs); console.log(f);Answer
undefined
An empty array has no item at index zero, so the function returns
undefined. -
What does this program print?
const nums: number[] = [1, 2, 3]; const doubled: number[] = nums.map((n) => n * 2); console.log(doubled.join(", "));Answer
2, 4, 6
mapbuilds a new array from the returned values, andjoinputs the separator between them. -
What does this program print?
function first<T>(items: T[]): T | undefined { return items[0]; } console.log(first(["a", "b"]));Answer
a
The strings make
Ta string, and the first item is printed. -
What does the compiler say about this program?
function first<T>(items: T[]): T | undefined { return items[0]; } console.log(first<number>(["a"]));Answer
error TS2322: Type 'string' is not assignable to type 'number'.
The explicit argument fixes
Tasnumber, so the string item no longer fits. -
What does the compiler say about this program?
function pick<T>(item: T): string { return item.name; } console.log(pick({ name: "ada" }));Answer
error TS2339: Property 'name' does not exist on type 'T'.
Tmay be any type, so the body cannot read a property that only some types carry. -
What does
Tbecome in the callfirst([1, 2, 3])?Answer
numberThe parameter is
T[], so the array of numbers makes the item type, andT, a number.
Article author: Arthur Isaev