8.3 Constructor Declarations

A constructor declaration declares the constructor function of a class.

  ConstructorDeclaration:   AccessibilityModifieroptconstructor(ParameterListopt){FunctionBody}   AccessibilityModifieroptconstructor(ParameterListopt);

Constructor declarations that specify a body are called constructor implementations and constructor declarations without a body are called constructor overloads. It is possible to specify multiple constructor overloads in a class, but a class can have at most one constructor implementation. All constructor declarations in a class must specify the same set of modifiers. Only public constructors are supported and private or protected constructors result in an error.

In a class with no constructor declaration, an automatic constructor is provided, as described in section 8.3.3.

When a class has constructor overloads, the overloads determine the construct signatures of the type given to the constructor function object, and the constructor implementation signature (if any) must be assignable to that type. Otherwise, the constructor implementation itself determines the construct signature. This exactly parallels the way overloads are processed in a function declaration (section 6.2).

When a class has both constructor overloads and a constructor implementation, the overloads must precede the implementation and all of the declarations must be consecutive with no intervening grammatical elements.

The function body of a constructor is permitted to contain return statements. If return statements specify expressions, those expressions must be of types that are assignable to the this-type (section 3.6.3) of the class.

The type parameters of a generic class are in scope and accessible in a constructor declaration.

8.3.1 Constructor Parameters

Similar to functions, only the constructor implementation (and not constructor overloads) can specify default value expressions for optional parameters. It is a compile-time error for such default value expressions to reference this. When the output target is ECMAScript 3 or 5, for each parameter with a default value, a statement that substitutes the default value for an omitted argument is included in the JavaScript generated for the constructor function.

A parameter of a ConstructorImplementation may be prefixed with a public, private, or protected modifier. This is called a parameter property declaration and is shorthand for declaring a property with the same name as the parameter and initializing it with the value of the parameter. For example, the declaration

  1. class Point {
  2. constructor(public x: number, public y: number) {
  3. // Constructor body
  4. }
  5. }

is equivalent to writing

  1. class Point {
  2. public x: number;
  3. public y: number;
  4. constructor(x: number, y: number) {
  5. this.x = x;
  6. this.y = y;
  7. // Constructor body
  8. }
  9. }

A parameter property declaration may declare an optional parameter (by including a question mark or a default value), but the property introduced by such a declaration is always considered a required property (section 3.3.6).

8.3.2 Super Calls

Super calls (section 4.9.1) are used to call the constructor of the base class. A super call consists of the keyword super followed by an argument list enclosed in parentheses. For example:

  1. class ColoredPoint extends Point {
  2. constructor(x: number, y: number, public color: string) {
  3. super(x, y);
  4. }
  5. }

Constructors of classes with no extends clause may not contain super calls, whereas constructors of derived classes must contain at least one super call somewhere in their function body. Super calls are not permitted outside constructors or in local functions inside constructors.

The first statement in the body of a constructor must be a super call if both of the following are true:

  • The containing class is a derived class.
  • The constructor declares parameter properties or the containing class declares instance member variables with initializers.

In such a required super call, it is a compile-time error for argument expressions to reference this.

Initialization of parameter properties and instance member variables with initializers takes place immediately at the beginning of the constructor body if the class has no base class, or immediately following the super call if the class is a derived class.

8.3.3 Automatic Constructors

If a class omits a constructor declaration, an automatic constructor is provided.

In a class with no extends clause, the automatic constructor has no parameters and performs no action other than executing the instance member variable initializers (section 8.4.1), if any.

In a derived class, the automatic constructor has the same parameter list (and possibly overloads) as the base class constructor. The automatically provided constructor first forwards the call to the base class constructor using a call equivalent to

  1. BaseClass.apply(this, arguments);

and then executes the instance member variable initializers, if any.