Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference Β· Keywords and Declarations

TypeScript super keyword

Reference for super in TypeScript: calling a base constructor, calling overridden methods, accessing static parents, ordering rules and common errors.

super refers to the parent class of the class you are writing. As a function call, super(...) runs the base constructor. As a property access, super.method() calls the version of a method that the base class defines, even if the current class overrides it.

TypeScript checks both uses: the arguments of super(...) must match the base constructor's parameters, and super.name must name a method that exists on the base class.

TypeScript
class Child extends Parent {
  constructor() {
    super();              // base constructor
  }
  run() { super.run(); }  // base method
}

Try it

TypeScript super in constructors and methods

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 Forgetting super

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
super(arg1, arg2);        // inside a derived constructor
super.method(args);       // inside methods, accessors, static methods
super.property;           // read an accessor or method of the base prototype
Form Description
super(...) Calls the base constructor with those arguments. It returns this. Only valid in a constructor of a class with extends.
super.member Looks up member on the base class prototype (instance code) or the base class itself (static code).

Errors

  • A derived constructor that never calls super: error TS2377 ("Constructors for derived classes must contain a 'super' call").
  • Using this before super(...): error TS17009.
  • super(...) in a class with no extends: error TS2335 (β€œsuper can only be referenced in a derived class”).
  • super.field for a field (not a method or accessor): error TS2340, because fields live on instances, not the prototype.

Usage notes

  • Order. Field initializers of the derived class run right after super(...) returns, so derived fields cannot be read by the base constructor. Calling an overridable method from a base constructor therefore sees a half-built object.
  • Statements before super are allowed if they do not touch this (since TypeScript 4.6). Parameter properties and initialized fields still run only after the call.
  • Implicit constructors. A derived class with no constructor gets one that forwards all arguments to super.
  • Arrow functions inside a constructor or method may use super because they capture it lexically.
  • Fields cannot be reached through super. Use this.field. If a derived class redeclares a field, useDefineForClassFields controls whether the base value is overwritten or shadowed.
  • override. With noImplicitOverride, mark overriding members with the override modifier; it pairs naturally with calling super.method().
  • Targets before ES2015 emit _super.prototype.method.call(this), with identical behavior.