Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Classes, Enums and Decorators

TypeScript class constructors

Reference for TypeScript class constructors: typed parameters, overloads, calling super, access modifiers on constructors, and construct signatures.

A constructor is the special method named constructor that runs when you create an instance with new. It receives typed parameters, assigns the fields, and implicitly returns the new object. A class has at most one constructor implementation, but TypeScript lets you declare several overload signatures above it.

Constructors cannot declare a return type: the type of new C(...) is always the instance type of the class. In a derived class, the constructor must call super(...) before touching this.

TypeScript
class Point {
  constructor(public x: number, public y: number) {}
}
const p = new Point(1, 2);

Try it

TypeScript Overloaded constructor

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 super and private constructors

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
class C {
  [private | protected | public] constructor(params);          // overload signature
  [private | protected | public] constructor(params) { body }  // implementation
}
Part Description
Parameters Typed like function parameters; optional, default and rest parameters are allowed. Modifiers on a parameter turn it into a parameter property.
Overload signatures Zero or more declarations without bodies, followed by one implementation whose signature is not visible to callers.
Access modifier private blocks new outside the class; protected allows it only in subclasses.

Errors and edge cases:

  • A derived class with its own constructor must call super(); otherwise TS2377 is reported. When it declares parameter properties or initialized fields, super() must be the first statement.
  • A constructor cannot have type parameters of its own; generics belong on the class (class Box<T> { constructor(public value: T) {} }).
  • If you omit the constructor in a derived class, one that forwards all arguments to super is supplied.

Usage notes

  • The implementation signature must be compatible with all overloads but is hidden from callers. Handle the combinations with a union parameter and a runtime check.
  • The type typeof C is the constructor function type. Write new (...args: any[]) => T or { new (x: number): T } to describe a construct signature, for instance for factories.
  • Private constructors enable singletons and static factory methods; combined with static create() they give you named constructors.
  • Avoid heavy work or asynchronous calls in constructors, since they cannot be awaited. Use a static async create() method.
  • Field initializers run before the constructor body in a base class, and right after super() returns in a derived class.