TypeScript Reference ยท Generics
Generic interfaces in TypeScript
Reference for TypeScript generic interfaces: add type parameters to an interface, implement and extend them, and describe generic call signatures.
A generic interface has type parameters in angle brackets after its name. Every use of the interface supplies type arguments, so one declaration describes a whole family of shapes: a Box<number>, a Box<string>, a Repository<User>.
The type parameters can appear in property types, method signatures, call signatures and index signatures. Classes can implement a generic interface and other interfaces can extend it, passing their own type parameters along.
interface Box<T> { value: T; map<U>(fn: (v: T) => U): Box<U>; }
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
interface Name<T, U extends Constraint = Default> extends Base<T> { prop: T; method(arg: U): T; <V>(value: V): V; // generic call signature new (arg: T): Name<T, U>; // construct signature [key: string]: T; // index signature }
| Name | Description |
|---|---|
T, U |
Type parameters of the interface. Supplied at every use: Name<number, string>. |
extends Constraint |
Optional limit on what may be passed. |
= Default |
Optional default. Parameters with defaults must come after those without. |
<V> on a member |
A type parameter belonging to that member only, chosen at the call. |
Edge cases: you cannot use a generic interface without type arguments unless all parameters have defaults. Interfaces with the same name merge, and the merged declarations must declare identical type parameters.
Usage notes
- Interface-level versus member-level parameters.
interface A<T> { f(x: T): T }fixesTfor the whole object;interface B { f<T>(x: T): T }lets each call tofpick its ownT. - Variance matters. Using
Tonly in output positions (properties you read) makes the interface covariant; using it as a method parameter or callback argument changes how assignability works (see Variance and assignability). - Aliases work too.
type Box<T> = { value: T }describes the same shape for plain objects. Only interfaces can merge declarations; aliases can also name unions and other non-object types. - Declaration merging lets libraries extend a generic interface, but every declaration must repeat the same parameter list and constraints.
- Names in errors. Interfaces keep their name in error messages (
Box<string>), which is clearer than an expanded object type. - Do not add a parameter you never use. An unused
Tis not an error by default, but it makesBox<string>andBox<number>structurally identical.