Creating Classes That Implement An Interface In TypeScript

[!INFO]-
topic: πŸ¦’ TypeScript
links: Interfaces in TypeScript
source: TypeScript Course by the native web
tags: #permanent-noteΒ #published

Last Modified: =dateformat(this.file.mtime, "MMM. dd, yyyy - HH:mm")


Interfaces can extend other existing interfaces.
Classes can implement interfaces.

interface Animal {
  name: string;
  breathe: () => void;
}

interface AnimalWithLegs extends Animal {
  numberOfLegs: number;
}

class Dog implements AnimalWithLegs {
  name: string;
  numberOfLegs: number;

  constructor (name: string) {
    this.name = name;
    this.numberOfLegs = 4;
  }

  bark (): void {
    setTimeout(() => {
      console.log(`Woof woof, I'm ${this.name}!`);
    }, 1_000);
  }

  breathe (): void {
    console.log('In and out, and in and out, and in and out, ...');
  }
}

const alice = new Dog('Alice');

alice.bark();