Skip to content
Tutorials Center
My learning

Topics

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.

TypeScript
interface Box<T> {
  value: T;
  map<U>(fn: (v: T) => U): Box<U>;
}

Try it

TypeScript A generic repository interface

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.

TypeScript Call signatures, defaults and extension

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

TypeScript
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 } fixes T for the whole object; interface B { f<T>(x: T): T } lets each call to f pick its own T.
  • Variance matters. Using T only 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 T is not an error by default, but it makes Box<string> and Box<number> structurally identical.