TypeScript theorytheory 0/50 · 0%
Foundations · easy

13. Interfaces implemented by classes

Contracts a class promises to fulfil.

A class can `implements` an interface, promising to provide all its members. This is checked at compile time only — `implements` adds no runtime behaviour.

ts
interface Transferable {
  transfer(to: string, amount: number): boolean;
}

class Token implements Transferable {
  balances: Record<string, number> = {};
  transfer(to: string, amount: number): boolean {
    this.balances[to] = (this.balances[to] ?? 0) + amount;
    return true;
  }
}

A class can implement multiple interfaces (`class X implements A, B`), and interfaces can extend other interfaces, letting you compose small, focused contracts (e.g. `Mintable`, `Burnable`, `Transferable`) into a richer one.

Check your understanding

  1. 1. What does `implements` check?

  2. 2. Can a class implement more than one interface?

  3. 3. Does `implements` add runtime code?