Skip to content

Generic functions — a type parameter inferred from the arguments

Level: 201 · for anyone who has typed a function in TypeScript

Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The questions below are what the finished page has to answer.

One line: The caller never writes T: tsc infers it from the arguments, so pair(1, 2) returns number[]. Disagreeing arguments do not make a union — pair(1, "x") is an error, because tsc takes number from the first argument and checks the second against it.

Keywords: generics, type parameter, type argument inference

What the finished page will answer

  • What type does tsc infer for T in pair(1, 2), pair("a", "b") and first([]), and how can you see an inferred type without an editor?
  • Why is pair(1, "x") an error instead of T = string | number, and why does swapping the arguments swap the message?
  • Why is id("red") typed "red" when assigned to a const but string when assigned to a let?
  • What does writing the type argument yourself, pair<string | number>(1, "x"), change?
  • What is left of T once Node strips the types from function first<T>(xs: T[]): T | undefined?

Examples it will need

  • generic_functions_inferred_sh.sh — the .d.ts that tsc emits for calls to pair, id and first, one declaration per call with the type inferred there
  • generic_functions_disagree_tserror.ts — tsc's two TS2345 errors for pair(1, "x") and pair("x", 1), one naming number and the other string
  • generic_functions_erased_ts.ts — first applied to numbers, strings and an empty array, the same JavaScript running for every T

See also

Sources to start from