TypeScript Reference ยท Keywords and Declarations
TypeScript new operator and construct signatures
Reference for new in TypeScript: creating instances, construct signatures in types, constructor types, new.target, abstract classes and common errors.
new creates an object by calling a constructor. TypeScript uses the keyword in two ways: as the runtime operator (new Date()), and inside types as a construct signature (new (...args) => T) that describes "something you can call with new".
Construct signatures let you pass classes around as values, write factories and dependency-injection helpers, and describe how a class expression or a JavaScript constructor function behaves.
const d = new Date(0); 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.
Syntax
new Ctor(args) // operator new Ctor // parentheses optional with no arguments type T1 = new (a: A, b: B) => R; // constructor type type T2 = abstract new () => R; // abstract constructor type interface I { new (a: A): R } // construct signature in an interface
| Part | Description |
|---|---|
Ctor |
An expression whose type has a construct signature: a class, a constructor type, or an interface with new (...). |
new (params) => R |
A function type that must be invoked with new and produces an R. |
abstract new |
A constructor type that also accepts abstract classes (TypeScript 4.2+). |
Errors and edge cases
newon an abstract class: error TS2511 ("Cannot create an instance of an abstract class").newon a plain function: error TS7009 undernoImplicitAny, because the result is implicitlyany. Write a class instead.- Calling a class without
new, or an arrow function withnew, is an error (TS2348 and TS2351). - A construct signature and a call signature can coexist in one type, as with the built-in
Date,ArrayandErrorconstructors.
Usage notes
typeof Classgives the constructor type, including statics.InstanceType<typeof Class>recovers the instance type andConstructorParameters<typeof Class>the argument tuple.- Mixins use
type Constructor<T = {}> = new (...args: any[]) => T. Theany[]is deliberate: a mixin must accept whatever constructor the base class has. new.targetis available inside constructors and has the type of the constructor (orundefinedin functions called withoutnew). It is useful for abstract-like checks at runtime.- Generic constructors:
new <T>(value: T) => Box<T>is a generic construct signature; the class formclass Box<T>is equivalent for most uses. - Overloaded constructors are written as several constructor signatures above one implementation; only the signatures are visible.
- Return values: a constructor that returns an object replaces the new instance in JavaScript. TypeScript does not let you annotate a constructor's return type.
- Private and protected constructors make
newunavailable outside the class, enabling singleton and factory patterns.