TypeScript Reference ยท Generics
Generic functions in TypeScript
Reference for TypeScript generic functions: declare type parameters on functions, arrow functions and methods, and let the compiler infer them from arguments.
A generic function declares one or more type parameters in angle brackets after its name. The parameters act as placeholders: inside the function you use them in parameter and return types, and each call fills them in with real types. This lets one function work with many types while keeping the relationship between input and output precise.
Without generics you would pick between duplicating the function for every type or typing it with any and losing all checking. A generic identity<T>(value: T): T promises that what comes out has the same type as what went in.
function identity<T>(value: T): T { return value; } const n = identity(42); // number, inferred from the argument const s = identity<string>("hi"); // explicit type argument
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
function name<T, U = Default>(param: T, other: U): T; const arrow = <T,>(value: T): T => value; interface Callable { <T>(value: T): T } type Fn = <T>(value: T) => T;
| Name | Description |
|---|---|
T, U |
Type parameters. Any identifier works; T, U, K, V are conventions, and descriptive names such as TItem are fine. |
extends C |
Optional constraint that limits which types are accepted (see Generic constraints). |
= D |
Optional default used when nothing can be inferred (see Default type parameters). |
Returns: whatever the signature says, expressed in terms of the type parameters.
Edge cases: if there is nothing to infer from, T becomes unknown (or its constraint). If you give explicit type arguments you must give all of them, except those with defaults.
Usage notes
- Type parameters exist only at compile time. They are erased, so you cannot write
new T()ortypeof Tat runtime. Pass a constructor or a value if you need runtime information. - Prefer inference. Writing
first<number>(xs)is rarely needed; if the compiler infers it correctly, leave it out. - Use each type parameter at least twice (for example in a parameter and the return type). A parameter that appears once, such as
function log<T>(x: T): void, adds nothing overunknown. - Arrow functions in
.tsxfiles need<T,>or<T extends unknown>because<T>looks like a JSX tag. In.tsfiles plain<T>works. - Do not lie with return types.
function make<T>(): T { return {} as T }compiles but is unsound. - Overloads versus generics. A generic signature with a conditional return type can replace several overloads, but overloads are often easier to read.
- Function type syntax.
type Fn = <T>(x: T) => Tis a generic function type: the caller picksT.type Fn<T> = (x: T) => TfixesTwhen the alias is used.