Skip to content

L12 Interfaces and dispatch

19 minSuggested first: L11 Callbacks and async order

Outcome

Identify the declared contract, selection rule, and concrete implementation for one configuration.

Learn

An interface describes what an object offers. Dispatch is the decision about which implementation handles a call. A registry maps names to implementations.

To trace a dispatched call, follow the configuration to the selected object, then read its method. Go-to-definition may land on the interface declaration rather than the implementation that actually runs.

Worked example

What does load("memory") return, and which line decided that?

TypeScript
type Driver = { read(): string };
const registry: Record<"memory" | "disk", Driver> = {
  memory: { read: () => "temporary" },
  disk: { read: () => "persistent" },
};
function load(mode: "memory" | "disk") {
  return registry[mode].read();
}
Predict first, then show the worked explanation

With mode = "memory", the lookup registry[mode] on line 7 returns the memory object, and its read runs, returning "temporary". The Driver type tells you the result is a string; the argument tells you which body produced it.

Practice and check

Guided practice

Practice, not scored

Which object receives the read() call for load("disk")?

Choose one answer

Exit check

Counts toward completion

Without knowing the value of mode, which conclusion follows from the code?

Choose one answer

Common mistake

Using a method with the same name as evidence of dispatch. Find the actual object or token chosen at the call site.

Practice on a real repository

After the group checkpoint, try one of the puzzles below.

Related topics: Extension points

Practice is optional. Solving a puzzle doesn't complete a lesson, and lessons never change a puzzle's points.

References