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.

  1. What is a type parameter such as T in function 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.

  2. How does the compiler pick T in the call first([1, 2, 3])?

    Answer

    It infers number from the array argument

    The argument says the items are numbers, so the inferred type argument is number.

  3. 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.

  4. 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.

  5. 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.

  6. 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

    map builds a new array from the returned values, and join puts the separator between them.

  7. 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 T a string, and the first item is printed.

  8. 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 T as number, so the string item no longer fits.

  9. 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'.

    T may be any type, so the body cannot read a property that only some types carry.

  10. What does T become in the call first([1, 2, 3])?

    Answer

    number

    The parameter is T[], so the array of numbers makes the item type, and T, a number.

Article author: Arthur Isaev