Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Generics

Constructor and factory types in TypeScript

Reference for TypeScript constructor types: new signatures, generic factory functions, abstract constructors, InstanceType and mixin-style class factories.

A constructor type describes a value you can call with new. Because type parameters disappear at runtime, a generic function cannot write new T(). Instead it accepts a constructor as an argument and uses a constructor type to say what that constructor builds. This is the basis of dependency containers, factories, registries and mixins.

A constructor type is written like a function type with new in front: new (...args: A) => T, or as an object type with a construct signature: { new (...args: A): T }.

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

function create<T>(ctor: Constructor<T>): T {
  return new ctor();
}

Try it

TypeScript A generic factory

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 Registry and mixin

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 (...args: any[]) => T;
type Ctor2<T> = { new (name: string): T };
type AbstractCtor<T> = abstract new (...args: any[]) => T;
type Args<C extends abstract new (...args: any) => any> = ConstructorParameters<C>;
type Made<C extends abstract new (...args: any) => any> = InstanceType<C>;
Name Description
new (...) => T A value callable with new that produces T.
abstract new (...) => T Also accepts abstract classes, which cannot be instantiated directly.
typeof ClassName The type of the class constructor itself (static side).
ConstructorParameters<C> The tuple of constructor parameter types.
InstanceType<C> The instance type produced by the constructor.

Edge cases: passing an abstract class where new (...) => T is expected is an error; use the abstract new form to accept it. any[] in the arguments is the idiomatic way to accept any constructor in a mixin.

Usage notes

  • Pass the class, not an instance. create(Logger), not create(new Logger()).
  • typeof Class versus Class. Logger as a type means the instance; typeof Logger means the constructor with its static members.
  • Mixins return a class expression extending the argument. The base needs a constructor with any[] rest parameters, and the result's static side is inferred.
  • Constructors are not callable without new. A class cannot be invoked like a function; if both work, declare both a call and a construct signature.
  • Prefer interfaces for registries when you need static factory methods: interface Factory<T> { create(): T }.
  • No new T(). Type parameters do not exist at runtime, so always take the constructor as a value.