Liskov Substitution Principle in TypeScript

liskov-substitution-principle-in-typescript

When extending a class, remember that you should be able to pass objects of the subclass in place of objects of the parent class without breaking the client code.

The goal of this principle is that subclasses remain compatible with the behavior of the parent class. Subclasses should extend the behavior of the parent class and not replace it by something different.

If you follow this principle you will be able to replace a parent class by any of its subclasses without breaking the client code.

Imagine that we have an application that accepts orders. There are two possible states for an order: draft or confirmed. If an order was not confirmed, it cannot be payed.

In the following example we are breaking the substitution principle because the parent class has the method markAsPaid which does not throw any errors. On the contrary, the subclass DraftOrder throws an error in that method because draft orders cannot be payed. Replacing the parent class Order by its subclass DraftOrder may break the code if we were calling markAsPaid.

class Order {
  id: number;
  items: string[];
  payed: boolean;

  // constructor

  markAsPaid(): void {
    this.payed = true;
  }
}

class DraftOrder extends Order {
  markAsPaid(): void {
    throw new Error("Draft orders can't be payed");
  }
}

We can improve this by making draft orders the parent class and confirmed orders the subclass. This way it is possible to replace the parent class by the subclass without breaking the code.

class Order {
  id: number;
  items: string[];

  // constructor
}

class ConfirmedOrder extends Order {
  payed: boolean;

  // constructor

  markAsPaid(): void {
    this.payed = true;
  }
}
Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
how-to-promote-your-youtube-channel-[+-tips-from-hubspot’s-youtube-team]

How to Promote Your YouTube Channel [+ Tips from HubSpot’s YouTube Team]

Next Post
18-best-b2b-website-examples-&-how-to-design-a-great-b2b-website

18 Best B2B Website Examples & How to Design a Great B2B Website

Related Posts