Skip to content
Tutorials Center
My learning

Topics

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.

TypeScript
type Constructor<T> = new (...args: any[]) => T;

Try it

TypeScript A factory that takes a class

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 Mixins and abstract constructors

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 Abstract class as a plain constructor

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
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 ClassName is the type of the class value, which includes static members and the construct signature. ClassName alone is the instance type.
  • InstanceType<T> and ConstructorParameters<T> extract the instance type and the parameter tuple from a construct signature.
  • The Constructor<T> alias with any[] parameters is the standard base for mixins. TypeScript requires mixin constructors to take a single rest parameter of type any[].
  • 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, Map and Error work in registries as in the first example.
  • Prefer a plain factory function type () => T when the caller does not need new semantics or instanceof.