TypeScript Reference ยท Generics
Generic classes in TypeScript
Reference for TypeScript generic classes: type parameters on classes, constraints, generic methods, static members and extending or implementing generic types.
A generic class declares type parameters after its name. Instances are created with concrete type arguments, so the same class can hold numbers in one place and users in another while the compiler checks each use. Containers such as stacks, queues, caches and repositories are the classic examples.
Type parameters are available in property types, constructor parameters, method signatures and method bodies, but only on the instance side: static members cannot refer to the class's type parameters.
class Stack<T> { private items: T[] = []; push(item: T): void { this.items.push(item); } pop(): T | undefined { return this.items.pop(); } }
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
class Name<T, U extends Constraint = Default> extends Base<T> implements Iface<U> { static create<V>(value: V): Name<V, string> { /* ... */ } method<W>(arg: W): T { /* ... */ } }
| Name | Description |
|---|---|
T, U |
Type parameters of the class, available to all instance members. |
extends Constraint |
Optional limit on accepted type arguments. |
= Default |
Optional default type argument. |
extends Base<T> |
A base class that is itself generic; pass type arguments or your own parameters. |
Edge cases: static members cannot use the class's T (error TS2302: "Static members cannot reference class type parameters"), because statics belong to the constructor, not to any one instance type. A static generic method may declare its own parameters.
Usage notes
- Inference from the constructor.
new Box("a")infersBox<string>. When the constructor gives no hints, write the argument yourself:new Stack<number>(). Without it,Tbecomesunknown. - Properties assigned in the constructor may need
!or an initializer understrictPropertyInitialization. thisreturn types keep fluent chains typed when a subclass inherits the method.- Type parameters are erased. You cannot write
new T()or checkx instanceof T. Accept a constructor (ctor: new () => T) if you need to build instances. - Variance. A mutable container such as
Stack<Dog>is assignable toStack<Animal>in TypeScript because method parameters are checked bivariantly, which is convenient but unsound. See Variance and assignability. - Constrain for the members you use. Without
extends HasId,item.idwould be an error insideRegistry.