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?
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
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.
- Registering a new built-in driverArchitecture · Easy · unjs-unstorage
- Routers that can ask SmartRouter to fall backArchitecture · Easy · honojs-hono
Related topics: Extension points
Practice is optional. Solving a puzzle doesn't complete a lesson, and lessons never change a puzzle's points.
References
- Narrowing (TypeScript Handbook)
- SCIP Code Intelligence Protocol (SCIP maintainers)