Skip to content

Commit

Permalink
#28 - TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
Sac-Corts committed Sep 25, 2024
1 parent 23df267 commit 920c965
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions Roadmap/28 - SOLID LSP/typescript/Sac-Corts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Wrong way
class Bird {
fly(): void {
console.log('Flying!');
}
}

class Penguin extends Bird {
fly(): void {
throw new Error('Penguins cannot fly!');
}
}

function makeBirdFly(bird: Bird): void {
bird.fly();
}

const bird = new Bird();
const penguin = new Penguin();

makeBirdFly(bird);
// makeBirdFly(penguin); // Not working //

// Correct way
class Bird2 {
eat(): void {
console.log('Eating...');
}
}

class FlyingBird extends Bird2 {
fly(): void {
console.log('Flying!');
}
}

class Penguin2 extends Bird2 {
swim(): void {
console.log('Swimming!');
}
}

function makeFlyingBirdFly(bird: FlyingBird): void {
bird.fly();
}

const eagle = new FlyingBird();
const penguin2 = new Penguin2();

makeFlyingBirdFly(eagle);
// makeFlyingBirdFly(penguin2); // Not working //

// ** Extra Exercise ** //
abstract class Vehicle {
abstract accelerate(): void;
abstract brake(): void;
}

class Car extends Vehicle {
accelerate(): void {
console.log('Car is accelerating');
}

brake(): void {
console.log('Car is braking');
}
}

class Motorcycle extends Vehicle {
accelerate(): void {
console.log('Motorcycle is accelerating');
}

brake(): void {
console.log('Motorcycle is braking');
}
}

class Bicycle extends Vehicle {
accelerate(): void {
console.log('Bicycle is accelerating');
}

brake(): void {
console.log('Bicycle is braking');
}
}

function testVehicle(vehicle: Vehicle): void {
vehicle.accelerate();
vehicle.brake();
}

const car = new Car();
const motorcycle = new Motorcycle();
const bicycle = new Bicycle();

testVehicle(car);
testVehicle(motorcycle);
testVehicle(bicycle);

0 comments on commit 920c965

Please sign in to comment.