Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Classes, Enums and Decorators

TypeScript abstract classes

Reference for TypeScript abstract classes: abstract methods and properties, the template method pattern, instantiation errors and abstract construct signatures.

An abstract class is a class marked with abstract that exists to be extended. You cannot create an instance of it with new. It can contain ordinary members with implementations and abstract members that have no body, which every concrete subclass must provide.

Abstract classes sit between an interface (types only) and a normal class (complete). They emit real JavaScript, so instanceof works with them.

TypeScript
abstract class Report {
  abstract title(): string;
  print(): string { return "== " + this.title(); }
}

Try it

TypeScript Template method pattern

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 What the compiler rejects

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
abstract class A {
  abstract prop: T;
  abstract method(): R;
  protected abstract hook(): void;
  concrete(): void { ... }
  constructor(...) { ... }
}
type AbstractCtor<T> = abstract new (...args: any[]) => T;
Part Description
abstract class Cannot be instantiated directly; may have constructors that subclasses call with super().
abstract members Methods, properties and accessors without implementation. Allowed only in abstract classes.
Modifiers abstract can combine with public, protected and readonly, but not with private or static.
abstract new A construct-signature type that accepts abstract classes (added in TypeScript 4.2).

Edge cases:

  • A concrete subclass that forgets an abstract member is an error at its declaration, not at use.
  • A subclass can stay abstract by repeating the abstract modifier and implement only some members.
  • Abstract members cannot be called through super in the subclass (error TS2513).

Usage notes

  • Constructors of abstract classes run when a concrete subclass is created, so they can set shared state. Do not call abstract methods from the constructor: the subclass fields are not initialized yet.
  • Use an abstract class when you need shared implementation plus required hooks. If you only need a contract, an interface is lighter and emits no code.
  • Types such as new () => T do not accept abstract classes; use abstract new () => T when you accept either kind.
  • abstract is erased from the output except that the class itself is kept; abstract members leave no trace in the JavaScript.
  • An abstract property declared with abstract readonly name: string can be implemented by a field, a getter or a parameter property.