Abstract Classes
An abstract class is a class you cannot new. It can include real method bodies and abstract method stubs that subclasses must implement. The abstract pieces are erased; the rest is a normal JavaScript class.
Detailed Explanation
Sometimes you want a shared implementation and a required hole. An interface can describe the hole but cannot hold a method body. A concrete class can hold the body but can be constructed with the hole still empty. An abstract class is the pair: a constructor you cannot call directly, plus methods marked abstract that have no body.
abstract class Store<T> {
abstract load(id: string): Promise<T>;
async loadOrThrow(id: string): Promise<T> {
const row = await this.load(id);
if (row === undefined) throw new Error(id);
return row;
}
}
new Store() is a type error. class MemoryStore extends Store<User> { ... } is legal once every abstract member is implemented. The compiler checks the signatures. It does not check that the subclass calls super correctly beyond the usual constructor rules.
Abstract methods are a type-system feature. In the emit, they disappear; the subclass methods are ordinary prototype methods. Abstract fields exist too (abstract url: string) and mean "a subclass must declare this field." There is no runtime flag that a class is abstract. instanceof Store still works for subclasses, because the abstract class is a real constructor function.
Choose an abstract class when subclasses share code. Choose an interface (or a type) when they only share a shape — including when you want two unrelated classes to satisfy the same contract without a common base. TypeScript allows a class to implement an interface and extend an abstract class at the same time.
Abstract classes can have constructors, static members, visibility, and generics. They can implement interfaces. They cannot be newed, even from inside the abstract class, unless you are in a context the checker accepts (rare). A function that wants "a subclass of Store" takes typeof a concrete class or a construct signature: new (url: string) => Store<T>.
The cost is the same as any inheritance: a change to the abstract class is a change to every subclass. The benefit is one implementation of the shared path and a compile error when a new subclass forgets a method.
Code Examples
1. Shared body, required hole.
// 1. Shared body, required hole.
abstract class Repo<T extends { id: string }> {
abstract findById(id: string): T | undefined;
require(id: string): T {
const row = this.findById(id);
if (!row) throw new Error(`missing ${id}`);
return row;
}
}
class MemoryRepo<T extends { id: string }> extends Repo<T> {
constructor(private rows: T[]) {
super();
}
findById(id: string): T | undefined {
return this.rows.find((row) => row.id === id);
}
}
const users = new MemoryRepo([{ id: "u1" }]);
console.log(users.require("u1").id); // u12. You cannot construct the abstract class.
// 2. You cannot construct the abstract class.
// const bad = new Repo<{ id: string }>();
// Error: Cannot create an instance of an abstract class.3. A factory that accepts a concrete subclass.
// 3. A factory that accepts a concrete subclass.
type RepoCtor<T extends { id: string }> = new (rows: T[]) => Repo<T>;
function make<T extends { id: string }>(Ctor: RepoCtor<T>, rows: T[]): Repo<T> {
return new Ctor(rows);
}
const repo = make(MemoryRepo, [{ id: "a" }]);Common Gotchas
Forgetting one abstract member fails on the subclass, not the base
the error lists the missing method. An extra method on the subclass is fine.
Abstractness is erased
JavaScript can still `new` the emitted constructor if it calls it directly. The guard is the type checker, not the engine.
Prefer an interface when there is no shared code
an abstract class with only abstract members is a heavier interface.
`typeof AbstractClass` is not a valid `new` target in types either
write a construct signature that returns the abstract instance type if you need to pass constructors around.