Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Classes, Enums and Decorators

TypeScript class inheritance

Reference for TypeScript class inheritance: extends, super calls, overriding methods, the override keyword, protected members and extending built-ins.

A class inherits from another with extends. The derived class gets the base class's fields and methods, can add its own, and can replace methods by declaring members with the same name. Inside the derived class, super refers to the base class.

An instance of the derived class is assignable to the base type, which makes inheritance the basis for subtype polymorphism. TypeScript checks that overrides stay compatible with the base declaration.

TypeScript
class Animal {
  speak(): string { return "..."; }
}
class Dog extends Animal {
  override speak(): string { return "Woof"; }
}

Try it

TypeScript Extending and overriding

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 Errors the checker catches

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 Derived extends Base<Args> implements I {
  constructor(...) { super(...); }
  override method() { super.method(); }
}
Part Description
extends Base Single inheritance. The expression may be any value that is a constructor, including a call that returns a class (a mixin).
super(...) Calls the base constructor. Required in a derived constructor, before the first use of this.
super.member Accesses a base-class method or accessor. Base-class fields are not reachable this way.
override Declares that a member replaces a base member. With noImplicitOverride, it becomes mandatory.

Edge cases:

  • An overriding method may accept fewer or more general parameters and return a more specific type, but not the reverse.
  • You cannot make a member less accessible: a public base member cannot become private in a subclass. You can widen protected to public.
  • A derived class that redeclares a field overrides the base declaration; its type must be assignable to the base one.

Usage notes

  • Prefer composition when the relationship is "has a" rather than "is a". Deep hierarchies make code hard to change.
  • Turn on noImplicitOverride so that renaming a base method does not silently turn an override into a new, unused method.
  • abstract classes force subclasses to implement members; see Abstract classes.
  • Extending built-ins such as Array or Error works when targeting ES2015+. For Error, set name yourself to get a useful class name in logs.
  • Methods are looked up on the prototype chain at runtime, so super.method() in an arrow-function field is not available.
  • Interfaces are not inherited at runtime; implements only checks shape (see Implementing interfaces).