Course contentsShow
← TypeScript
Lesson 242 of 1,7966. FunctionsFree lesson

Construct Signatures

Learn how to type constructor functions using construct signatures with the new keyword.

Construct Signatures

What You'll Learn: You'll learn how to type constructor functions using construct signatures with the new keyword.

In JavaScript, constructor functions are called with the new keyword to create instances of objects. TypeScript lets you describe these constructors using construct signatures, which look like call signatures but start with new.

The Core Idea

A construct signature describes a function that must be called with new. It specifies what types the constructor accepts as parameters and what type of object it creates.

The syntax mirrors call signatures, but with new before the parameter list:

type Constructor = {
  new (name: string, age: number): Person;
};

This says: "A constructor that takes a name string and age number, and returns a Person object."

Using Construct Signatures

type Person = {
  name: string;
  age: number;
};

type PersonConstructor = {
  new (name: string, age: number): Person;
};

function createPerson(ctor: PersonConstructor): Person {
  return new ctor("Alice", 30);
}

Notice how PersonConstructor describes something that can be called with new. The function createPerson accepts any constructor matching that signature.

Combining Call and Construct Signatures

A type can have both regular call signatures and construct signatures, describing something callable both ways:

type Callable = {
  (x: number): number;
  new (x: number): { value: number };
};

Key Takeaway: Construct signatures let you type constructor functions by adding new before the parameter list, ensuring type safety when creating object instances with the new keyword.