1. Concept library
  2. JavaScript & TypeScript
  3. Module 9: Classes and composition

Concept

Implementing interfaces

implements makes a class promise to match an interface, so the compiler checks every method and property is there with the right types. Code can then depend on the interface instead of the class.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain what implements promises and how the compiler checks it
  2. Write a class that implements an interface and a second implementation of it
  3. Depend on the interface in a function signature instead of the class

In practice

A few lines that show Implementing interfaces doing its job.

TypeScript
interface Storage {
  get(key: string): string | null;
  set(key: string, value: string): void;
}

class MemoryStorage implements Storage {
  private data = new Map<string, string>();
  get(key: string) { return this.data.get(key) ?? null; }
  set(key: string, value: string) { this.data.set(key, value); }
}

function save(store: Storage) { store.set("theme", "dark"); }

The common mistake

Watch for

Typing variables and parameters as the class, like store: MemoryStorage, after going to the trouble of an interface. Swapping in another implementation or a test double then fails to type-check.

What to know first

Your roadmap puts these before Implementing interfaces, unless you already know them.

How it stays learned

Once a session shows you understand Implementing interfaces, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled

Where it leads

Topics that build on Implementing interfaces.