Skip to content
Tutorials Center
My learning

Topics

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.

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

Try it

TypeScript Classes as values

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 Errors with new

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
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

  • new on an abstract class: error TS2511 ("Cannot create an instance of an abstract class").
  • new on a plain function: error TS7009 under noImplicitAny, because the result is implicitly any. Write a class instead.
  • Calling a class without new, or an arrow function with new, is an error (TS2348 and TS2351).
  • A construct signature and a call signature can coexist in one type, as with the built-in Date, Array and Error constructors.

Usage notes

  • typeof Class gives the constructor type, including statics. InstanceType<typeof Class> recovers the instance type and ConstructorParameters<typeof Class> the argument tuple.
  • Mixins use type Constructor<T = {}> = new (...args: any[]) => T. The any[] is deliberate: a mixin must accept whatever constructor the base class has.
  • new.target is available inside constructors and has the type of the constructor (or undefined in functions called without new). It is useful for abstract-like checks at runtime.
  • Generic constructors: new <T>(value: T) => Box<T> is a generic construct signature; the class form class 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 new unavailable outside the class, enabling singleton and factory patterns.