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
Tinpair(1, 2),pair("a", "b")andfirst([]), and how can you see an inferred type without an editor? - Why is
pair(1, "x")an error instead ofT = string | number, and why does swapping the arguments swap the message? - Why is
id("red")typed"red"when assigned to aconstbutstringwhen assigned to alet? - What does writing the type argument yourself,
pair<string | number>(1, "x"), change? - What is left of
Tonce Node strips the types fromfunction 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¶
- Constraints — limiting which types T may be
- When inference guesses wrong — when the arguments cannot, or should not, decide T
- Annotations and inference — inference for plain variables, the same machinery
- Higher-order functions — the functions generics are most often written for
- Rust: What a generic is ↗ — each Rust instantiation gets its own copy; TypeScript erases T entirely