Subtypes must be substitutable for their base type without breaking behavior.
// dirty — Square breaks Rectangle's contract
class Rectangle {
width = 0;
height = 0;
setWidth(w: number): void { this.width = w; }
setHeight(h: number): void { this.height = h; }
area(): number { return this.width * this.height; }
}
class Square extends Rectangle {
setWidth(w: number): void { this.width = w; this.height = w; } // breaks LSP!
setHeight(h: number): void { this.width = h; this.height = h; }
}Refactor using factory functions so createRectangle and createSquare are independent — no inheritance. Both expose area().
solve('square', 5) → 25 (side² )solve('rect', 4, 6) → 24 (width × height)
Sample tests