Sub topic 6
Constraining to Constructor Types
Assume you know basic TypeScript types, interfaces, type aliases, and generic functions. This chapter focuses on the instance side of a class and on the type of a value that can be called with new.
A constructor constraint is useful when a function receives a class and creates an instance from it. The function should accept classes that produce a particular type, rather than arbitrary values.
The constructor type
In TypeScript, a class has two related sides:
- The instance side describes values created by the class.
- The static side contains the class itself, including its constructor and static members.
For example:
class Point {
x = 0;
y = 0;
}
const point: Point = new Point();
const pointClass: typeof Point = Point;Point describes an instance. typeof Point describes the class value that can create an instance.
A construct signature describes a value that can be used with new:
type Constructor<T = object> = new (...args: any[]) => T;Read this as: Constructor<T> is a constructor that accepts any arguments and returns an instance of T.
The new keyword in the type is important. A normal function type uses (...) => T, while a constructor type uses new (...) => T.
Passing a class to a generic function
A generic function can accept a constructor and infer the created instance type from it:
type Constructor<T = object> = new (...args: any[]) => T;
function createInstance<T>(
ctor: Constructor<T>,
...args: any[]
): T {
return new ctor(...args);
}
class User {
constructor(
public name: string,
public active: boolean
) {}
}
class Point {
x = 0;
y = 0;
}
const user = createInstance(User, "Ada", true);
const point = createInstance(Point);
console.log(user.name); // Ada
console.log(user.active); // true
console.log(point.x); // 0TypeScript infers T from the constructor passed as ctor. In the first call, T is User. In the second call, T is Point.
The returned value keeps the specific class type. You can access user.name because the result is a User, not merely an object.
The any[] argument list makes this small factory flexible, but it does not check whether the arguments match each constructor's parameter list. A production API can use more advanced tuple types when it needs strict constructor-argument checking.
Constructor constraints and interfaces
You can constrain the instance type instead of accepting every possible instance type. For example, this factory accepts only constructors whose instances have a reset method:
interface Resettable {
reset(): void;
}
type ResettableConstructor = new (...args: any[]) => Resettable;
function createAndReset(
ctor: ResettableConstructor
): Resettable {
const value = new ctor();
value.reset();
return value;
}
class FormState implements Resettable {
reset(): void {
console.log("Form reset");
}
}
const form = createAndReset(FormState);
form.reset();The constraint guarantees that the function body can call value.reset(). A class without that method cannot be passed to createAndReset.
A constructor constraint can therefore enforce two facts at once:
| Part of the type | What it guarantees |
|---|---|
| `new (...args: any[])` | The value can be called with `new` |
| `=> Resettable` | The created instance has `reset()` |
A common error: using the instance type as a constructor
This function is incorrect:
function wrong<T>(ctor: T): T {
return new ctor();
}T has no constraint, so TypeScript cannot assume that ctor is constructable. The type parameter might be a string, object, or number.
Add a construct signature to state the required operation:
type Constructor<T> = new (...args: any[]) => T;
function correct<T>(ctor: Constructor<T>): T {
return new ctor();
}The generic parameter describes the result. The constructor type describes the input operation that produces that result.
Constructors with defaults
A default type parameter can make a constructor alias convenient when the exact instance type is not important:
type Constructor<T = object> = new (...args: any[]) => T;
function makeEmpty<T = object>(ctor: Constructor<T>): T {
return new ctor();
}
class Settings {
mode = "safe";
}
const settings = makeEmpty(Settings); // SettingsThe default applies only when TypeScript cannot infer T. Here, it infers Settings, so the default object is not used.
The same pattern works well for dependency injection, plugin registration, serializers, and object factories. Use a constructor constraint when the API needs to receive a class, create an instance, or inspect the instance's required methods.
Exercise
Why does function make<T>(ctor: T): T { return new ctor(); } fail, and what type should replace T for the parameter?
Show answer
It fails because an unconstrained T is not known to be constructable. Use a construct signature such as Constructor<T> = new (...args: any[]) => T for the parameter.