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.
class Child extends Parent { constructor() { super(); // base constructor } run() { super.run(); } // base method }
Try it
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.
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
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
thisbeforesuper(...): error TS17009. super(...)in a class with noextends: error TS2335 (βsuper can only be referenced in a derived classβ).super.fieldfor 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
superare allowed if they do not touchthis(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
superbecause they capture it lexically. - Fields cannot be reached through
super. Usethis.field. If a derived class redeclares a field,useDefineForClassFieldscontrols whether the base value is overwritten or shadowed. override. WithnoImplicitOverride, mark overriding members with theoverridemodifier; it pairs naturally with callingsuper.method().- Targets before ES2015 emit
_super.prototype.method.call(this), with identical behavior.