6.12 Type a collaborator by its contract: examples
Rule: §6.12 in the code standards.
Why
The type of a collaborator parameter is the contract at the seam: what production passes and what a test fake has to provide. Typed unknown and then cast with as, the parameter accepts anything, and a wrong argument shows up as a failed call at run time instead of a compile error. A Pick<…> of the real type narrows the contract to the members the callee uses, so a fake implements only those.
This applies to collaborators, the objects whose methods the callee calls. Data that crosses a boundary (a JSON payload, a tool argument) stays unknown and is narrowed with runtime checks, per §4.1.
Good: an interface with a Pick slice
src/transport/runtimeConnection.ts:43-55:
export interface RuntimeConnectionDeps {
createChannel: typeof realCreateChannel;
createHeartbeat: typeof realCreateHeartbeat;
registry: Pick<
typeof import("../registry.js"),
| "discoverRuntime"
| "normalizePath"
| "watchRegistry"
| "unwatchRegistry"
| "isWatcherActive"
| "getCachedRuntimePort"
>;
}
Bad: unknown, then a cast
src/registration/toolRefs.ts:13-17 is an existing case, listed in B7:
const toolRefs = new Map<string, ToolRef>();
export function setToolRef(name: string, ref: unknown): void {
toolRefs.set(name, ref as ToolRef);
}
Any value compiles at the call site. A ref without remove() throws only when the tool is removed, and removeToolByName (lines 30-34) catches that as a tool already removed, so the mistake never surfaces.
Source
- The project’s own rule against
asfor validation (§4.5), applied to collaborator parameters.