Course contentsShow
← TypeScript
Lesson 1051 of 1,79624. Classes in TypeScriptFree lesson

Abstract Properties

Requiring subclasses to define specific properties by marking them abstract.

Abstract Properties in TypeScript

What you'll learn: How to require subclasses to define specific properties by marking them abstract.

The Core Idea

Just as you can declare abstract methods that subclasses must implement, you can also declare abstract properties. These are property declarations without values in an abstract class that force every subclass to provide its own implementation of that property.

Think of it like a blueprint that says "every building based on this design must have a front door" — but each specific building decides what kind of door and where exactly it goes.

Basic Abstract Property Syntax

abstract class Vehicle {
  abstract maxSpeed: number;
  abstract fuelType: string;
}

class Car extends Vehicle {
  maxSpeed = 120;
  fuelType = "gasoline";
}

class Bicycle extends Vehicle {
  maxSpeed = 25;
  fuelType = "none";
}

In this example, the Vehicle class requires every subclass to define maxSpeed and fuelType, but doesn't prescribe what those values should be.

Why This Matters

Abstract properties enforce a contract: "If you extend this class, you must provide these specific pieces of data." This ensures consistency across all subclasses while allowing each one to supply its own values.

abstract class DatabaseConnection {
  abstract readonly connectionString: string;
  abstract timeout: number;

  connect(): void {
    console.log(`Connecting to ${this.connectionString}`);
  }
}

class PostgresConnection extends DatabaseConnection {
  readonly connectionString = "postgresql://localhost:5432";
  timeout = 5000;
}

Notice how the base class can even use abstract properties in concrete methods — TypeScript guarantees they'll exist.

Combining with Access Modifiers

Abstract properties can be combined with readonly and access modifiers:

abstract class Configuration {
  abstract readonly apiKey: string;
  protected abstract maxRetries: number;
}

class ProductionConfig extends Configuration {
  readonly apiKey = "prod-key-123";
  protected maxRetries = 3;
}

Key Takeaway: Abstract properties require subclasses to define specific properties, creating a type-safe contract that ensures all derived classes provide necessary data while allowing each to supply its own values.