TypeScript Reference ยท Literal and Object Types
TypeScript construct signatures
Reference for construct signatures in TypeScript: describing constructors with new, passing classes as values, factories, abstract constructors and InstanceType.
A construct signature describes something you can call with new. It is written like a call signature with the new keyword in front: { new (name: string): Animal }, or in compact form new (name: string) => Animal. The return type is the type of the instance that new produces.
Construct signatures let you treat classes as values: pass a class to a factory, store constructors in a registry, or write generic helpers such as create<T>(ctor: new () => T): T. A class declaration's value (the class itself, typeof Animal) is assignable to a matching construct signature.
type Constructor<T> = new (...args: any[]) => T;
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.
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
type Ctor<T> = new (a: A, b?: B) => T; interface Factory { new (name: string): Thing } type AbstractCtor<T> = abstract new (...args: any[]) => T; interface Both { (x: number): string; new (x: number): Thing } type Instance = InstanceType<typeof SomeClass>; type Params = ConstructorParameters<typeof SomeClass>;
| Part | Description |
|---|---|
new |
Marks the signature as constructible. |
| Parameters | The arguments the constructor accepts, with the same rules as function parameters. |
| Return type | The instance type. In the compact form it follows =>; in an object type it follows :. |
abstract new |
Describes a constructor that may be abstract; such a value can be extended but not instantiated directly. |
Errors: assigning an abstract class to a plain new () => T type is an error (2322, "Cannot assign an abstract constructor type to a non-abstract constructor type"). Calling a class without new is error 2348.
Usage notes
- The "Abstract class as a plain constructor" example fails on purpose and prints the real abstract constructor error.
typeof ClassNameis the type of the class value, which includes static members and the construct signature.ClassNamealone is the instance type.InstanceType<T>andConstructorParameters<T>extract the instance type and the parameter tuple from a construct signature.- The
Constructor<T>alias withany[]parameters is the standard base for mixins. TypeScript requires mixin constructors to take a single rest parameter of typeany[]. - Using a generic parameter list on the construct signature (
new <T>(x: T) => Box<T>) lets one constructor produce differently typed instances. - Overloaded constructors in a class become multiple construct signatures on
typeof Class. - Built-ins have construct signatures too, so
Date,MapandErrorwork in registries as in the first example. - Prefer a plain factory function type
() => Twhen the caller does not neednewsemantics orinstanceof.