All posts
  • Typescript
  • Best-practices
  • Solid

SOLID made simple

Clear examples of SOLID principles in TypeScript

johnykes logo

johnykes

5 min read

SOLID principles are fundamental design guidelines that help developers create more maintainable, flexible, and robust software. Let's explore each principle with practical TypeScript examples using a car-fleet mini-app.

S — Single Responsibility Principle (SRP)

A class should have only one reason to change.

In our car-fleet mini-app we want a place that only coordinates service operations on cars—nothing else.

// CarService.ts — ONLY handles service operationsexport class CarService {  constructor(private readonly notifier: NotificationPort) {} // injected (DIP)  performAnnualService(car: Car): void {    car.resetMaintenanceCounter();    this.notifier.send(`✅ ${car.vin} serviced`);  }}

CarService never touches persistence, UI, or inspection logic—it just performs the service workflow. If tomorrow the legal checklist changes, we edit only this class.

O — Open/Closed Principle (OCP)

Modules should be open for extension, closed for modification.

We start with petrol and electric cars. Later we add hydrogen without touching existing code:

// Car.ts - abstraction every concrete car implementsexport interface Car {  readonly vin: string;  drive(km: number): void;  fuelLevel(): number;  resetMaintenanceCounter(): void;}export class PetrolCar implements Car { /* … */ }export class ElectricCar implements Car { /* … */ }// ✨ new requirement: hydrogen vehicleexport class HydrogenCar implements Car { /* … */ }

Garage or CarService keep working because they depend only on the Car abstraction.

L — Liskov Substitution Principle (LSP)

Subtypes must be substitutable for their base type.

function logTrip(car: Car) {  car.drive(50);  console.log(`${car.vin}: ${car.fuelLevel().toFixed(1)} % left`);}logTrip(new ElectricCar("EV-001"));logTrip(new PetrolCar("PET-001")); // behaves correctly with same pre- & post-conditions

No caller needs to know (or care) which fuel type it's dealing with.

I — Interface Segregation Principle (ISP)

Many small, role-focused interfaces are better than one fat interface.

export interface Drivable { drive(km: number): void; }export interface Refuelable { refuel(litres: number): void; }export interface Rechargeable { recharge(kWh: number): void; }export class PetrolCar implements Drivable, Refuelable { /* … */ }export class ElectricCar implements Drivable, Rechargeable { /* … */ }

Each car implements only what it needs—battery vehicles aren't forced to stub refuel, and petrol cars don't fake a recharge.

D — Dependency Inversion Principle (DIP)

High-level modules and low-level modules both depend on abstractions.

Instead of hard-wiring an email sender inside CarService, we depend on a port:

// NotificationPort.tsexport interface NotificationPort {  send(message: string): void;}// EmailAdapter.ts — low-level detailexport class EmailAdapter implements NotificationPort {  constructor(private readonly smtp: SmtpClient) {}  send(msg: string): void { this.smtp.dispatchEmail("[email protected]", msg); }}// CarService.ts (revisited)export class CarService {  constructor(private readonly notifier: NotificationPort) {}  performAnnualService(car: Car): void { /* … */ }}

At runtime we inject new EmailAdapter(...) (or a Slack adapter tomorrow) without touching CarService.

Putting it together

SRP: CarService does one thing—service cars.

OCP: New car types (HydrogenCar) extend code, never modify it.

LSP: Any Car subclass works wherever a Car is required.

ISP: Split behaviour into Drivable, Rechargeable, etc., so classes implement only what they need.

DIP: High-level logic depends on simple ports (NotificationPort), not concrete email/SMS libraries.

More from the blog

1 min read

Welcome to NON FUNGIBLE CODE!

Who are we?

johnykes

Read more