BeCuriouzBeCuriouz

Learn something new every day

🔷 TypeScript
🤝

implements: A Class's Promise to the Compiler

💡 implements enforces that a class satisfies an interface's contract at compile time, but inherits no actual code — you still write every member yourself.

Recall how an interface describes the shape an object must have. The implements keyword connects that idea to classes: it tells TypeScript "this class promises to satisfy this interface's contract." If the class is missing a required property or method — or types one wrong — you get a compile-time error at the class itself, not later at some distant call site.

ts
interface Logger {
  prefix: string;
  log(message: string): void;
}

class ConsoleLogger implements Logger {
  prefix = "[app]";
  log(message: string) {
    console.log(`${this.prefix} ${message}`);
  }
}

Here the compiler checks ConsoleLogger against Logger. Forget log, or write log(message: number), and the error appears on the class line. A class can satisfy multiple interfaces at once — class C implements A, B — and it may still add extra members the interface never mentioned; the contract is a minimum, not a cap.

One crucial subtlety: implements is purely a compile-time check. It does not copy anything into the class — no default values, no method bodies. That's the key difference from extends, which actually inherits real implementation. With implements you must write every member yourself; the keyword only verifies you did.

🤝 implements: A Class's Promise to the Compiler — BeCuriouz