Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TypeScript · Const

abstract

TypeScript’s abstract modifier defines base classes and members that require subclass implementations. Abstract classes cannot be instantiated directly, but they can combine shared implementations with enforced subclass contracts.

Since
1.6

How it works

Use the abstract modifier to define a class intended only as a base for subclasses. Abstract members declare required contracts without implementations; concrete subclasses must implement them before they can be instantiated.

Syntax

abstract class ClassName {
  abstract memberName: Type;
  abstract methodName(...args: Args): ReturnType;
}

Key points

  • The modifier applies to classes and to class methods or fields.
  • Abstract members declare required structure while leaving implementation to derived classes.
  • Abstract methods and fields are only legal within an abstract class declaration.
  • They model reusable superclass behavior together with subclass-specific obligations.
  • A class with no abstract members is concrete, provided its other constraints are satisfied.

Common mistakes

  • An abstract class cannot be constructed directly; instantiate a concrete derived class instead.
  • A derived class must implement every inherited abstract member or itself be declared abstract.

Example: Implementing an abstract repository

abstract class Repository<T> {
  abstract findById(id: string): Promise<T | undefined>;

  log(result: T | undefined): void {
    console.log(result);
  }
}

class UserRepository extends Repository<User> {
  async findById(id: string): Promise<User | undefined> {
    return id === "u1" ? { id, name: "Ada" } : undefined;
  }
}

type User = { id: string; name: string };

const repository = new UserRepository();
repository.log(await repository.findById("u1"));

A concrete subclass implements the required abstract method while inheriting the base implementation.

When to use it

Template-style class hierarchies

A family of domain objects shares behavior but must provide different operations.

Enforced extension contracts

A framework needs to require lifecycle or data-access methods from integrations.

Shared implementation with variation

Common logging, validation, or state belongs in one reusable superclass.

Interview questions

QWhat is an abstract class?

An abstract class is a base class that cannot be instantiated directly. It can contain implemented members plus abstract members that subclasses must provide.

Follow-up: How does it differ from an interface? An abstract class can share implementation and state, while an interface describes a type contract without providing class implementations.

QWhat must a subclass do with abstract members?

An abstract method has a signature but no body. A concrete subclass must implement it, unless that subclass remains abstract.

Follow-up: What happens if the subclass omits it? TypeScript reports an error when the subclass is concrete; marking the subclass abstract defers the requirement.

QCan you instantiate an abstract class?

The compiler rejects new applied to an abstract class because the class is intended only to be extended. Instantiate a concrete subclass instead.

Follow-up: Can an abstract class be used as a variable type? Yes; a concrete subclass instance can be assigned to a variable typed as the abstract base class.

  • class
  • extends
  • implements
  • interface
  • abstract new construct signatures

Source: Documentation - Classes - TypeScript. Checked 2026-10-15.