Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Keywords and Declarations

TypeScript abstract classes and members

Reference for the TypeScript abstract keyword: abstract classes, abstract methods and properties, implementation rules, abstract constructor types and errors.

An abstract class is a base class that cannot be instantiated directly. It exists to be extended. Inside it you can declare abstract members: methods, properties and accessors that have a type but no implementation, and that every concrete subclass must provide.

This lets a base class implement shared behavior (a "template method") while forcing subclasses to supply the varying parts. Unlike an interface, an abstract class exists at runtime and can contain real code and state.

TypeScript
abstract class Shape {
  abstract area(): number;
  describe() { return `area ${this.area()}`; }
}

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 Abstract errors

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 Name {
  abstract prop: Type;
  abstract method(arg: A): R;
  abstract get computed(): T;
  concrete(): void { /* normal members allowed */ }
}
type AbstractCtor<T> = abstract new (...args: any[]) => T;
Part Description
abstract class Cannot be used with new. May have constructors, fields, concrete methods and abstract members.
abstract member A declaration with no body or initializer. Allowed only inside abstract classes.
Modifiers Can be combined with public, protected, readonly and override, but not with private or static.

Errors

  • new Animal(): error TS2511 ("Cannot create an instance of an abstract class").
  • A concrete subclass missing an implementation: error TS2515 ("Non-abstract class 'Cat' does not implement inherited abstract member speak from class 'Animal'").
  • Giving an abstract method a body is error TS1245; giving an abstract property an initializer is error TS1267.

Usage notes

  • Erased vs kept. The class itself is emitted as an ordinary class; the abstract keyword and abstract members vanish. Nothing stops JavaScript code from instantiating it, so do not rely on it as a security boundary.
  • Subclasses can stay abstract. A subclass may implement some members and leave others abstract if it is also declared abstract.
  • Calling abstract methods from the constructor is allowed but dangerous: the subclass fields are not initialized yet.
  • Abstract constructor types. typeof Animal is an abstract constructor. A parameter of type new () => Animal rejects it; use abstract new () => Animal (TypeScript 4.2+) when you only need the prototype or to subclass it (mixins).
  • Versus interface. Choose an interface when only the shape matters and several unrelated classes may adopt it. Choose an abstract class for shared code, protected state and a runtime instanceof check.
  • Abstract properties are not shared fields; each subclass declares and initializes its own.
  • override is optional when implementing an abstract member, even with noImplicitOverride.