One of the most useful questions in a code review is: could this mistake become impossible to compile?
I was examining a large AI analysis pipeline for maintainability. It already had a shared execution sequence, stage-specific actions, model configuration and tests. The problem was how many places needed to agree whenever a stage changed.
One small example stood out. Every stage needed an entry in a model-policy map. The code could describe a new stage without requiring that entry to exist. A forgotten update could survive compilation and fail when the pipeline looked up its policy.
The relationship was known. The type system simply hadn't been told about it.
A dictionary cannot tell you what you forgot
Here is a simplified version of the problem:
type Stage = "extract" | "verify" | "report" | "audit";
const modelPolicy: Record<string, string> = {
extract: "fast extraction model",
verify: "reasoning model",
report: "deterministic assembly",
};
There is no entry for audit, but this compiles.
Record<string, string> describes a dictionary with string keys and string values. It does not say that any particular stage must appear. An empty object would satisfy it too.
A runtime lookup might eventually throw a useful error. A coverage test might notice the omission earlier. Both are useful protections, but the underlying rule is simpler: this is a finite set of stages, and every member needs a policy.
We can express that directly:
type Stage = "extract" | "verify" | "report" | "audit";
const modelPolicy = {
extract: "fast extraction model",
verify: "reasoning model",
report: "deterministic assembly",
audit: "reasoning model",
} satisfies Record<Stage, string>;
Delete audit and the compiler reports the missing property. Add a new member to Stage and the map becomes incomplete until it has a corresponding entry.
The finite key union creates the obligation. satisfies checks the object against that obligation without replacing its inferred type with a broad annotation. A direct Record<Stage, string> annotation can also enforce completeness. The TypeScript documentation covers both the Record utility and the satisfies operator.
The relationship should have one owner
There is still a weakness if Stage is a second list maintained separately from the actual pipeline. Someone can forget to update both.
Where an authoritative definition already exists, derive the type from it:
const stages = [
{ key: "extract", label: "Extract evidence" },
{ key: "verify", label: "Verify claims" },
{ key: "report", label: "Assemble report" },
{ key: "audit", label: "Audit findings" },
] as const;
type Stage = (typeof stages)[number]["key"];
Now adding a stage to the definition changes the required keys of the policy map. The maintainer does not have to remember the connection; the compiler follows it.
This is a useful distinction when evaluating refactors. Moving two lists into one folder changes their location. Deriving one from the other changes how they can drift.
It also gives a precise target for duplication work: identify which definition owns the fact, then make dependent representations answer to it.
Why this matters when AI writes code
An instruction such as “when adding a stage, remember to update the model-policy map” depends on whoever makes the change retrieving that instruction and applying it correctly.
That applies to people as well as agents. With an AI coding agent, the instruction also competes for space with the task, surrounding source, tool results and other repository rules.
A compiler-enforced relationship reduces the amount of context needed to make a safe edit. The agent can still miss the dependency initially. When typechecking runs, the incomplete change produces a concrete error at the dependent definition.
That is a narrower and more useful feedback loop than asking the agent to reread every architectural document. The error identifies an obligation the edit has left unsatisfied.
Documentation still explains why a policy exists and how to choose the right value. The compiler handles whether the required entry exists at all.
Apply the same idea to states and outcomes
Complete maps are one application. Another is representing a result so its fields agree with its status.
Consider this shape:
type Result = {
success: boolean;
artifactId?: string;
error?: string;
};
It permits success without an artifact, failure without an error, or both fields at once. Every caller has to remember which combinations make sense.
A discriminated union expresses the intended alternatives:
type Result =
| { kind: "succeeded"; artifactId: string }
| { kind: "failed"; error: string };
function describeResult(result: Result): string {
switch (result.kind) {
case "succeeded":
return `Saved ${result.artifactId}`;
case "failed":
return result.error;
default: {
const unhandled: never = result;
return unhandled;
}
}
}
The required fields now follow from the result variant. The never check adds another obligation: if a new variant is introduced, this function must handle it. TypeScript documents this as exhaustiveness checking.
These techniques are particularly useful for lifecycle events, commands, permissions and other finite sets with explicit rules. An open-ended plugin registry or externally supplied configuration may need runtime checks instead. Start by deciding whether the set is actually closed.
Prove the restriction with a small pilot
Before spreading a new type pattern across a system, try it on one concrete failure mode.
For the policy map, the pilot is straightforward:
- Check that the complete map passes typechecking.
- Temporarily remove a required entry and confirm that it fails for that omission.
- Restore it, then add a temporary stage to the authoritative definition and confirm that its missing policy is reported.
- Remove the temporary changes and check that runtime behaviour remains unchanged.
The deliberately broken cases matter. A clean typecheck alone does not demonstrate that the new restriction catches the mistake it was designed to prevent.
Keep the pilot focused. Adding a completeness check does not require changing prompts, reorganising every stage or introducing a generic workflow framework. Expand it when the first example demonstrates a useful guarantee at a reasonable cost to readability.
Be precise about what the compiler proves
A complete policy map can still contain the wrong policy. An exhaustive handler can still implement the wrong behaviour. A well-typed model response can still make an unsupported claim.
Typechecking establishes structural consistency within the model we gave it. Runtime validation must establish that external data fits that model. Behavioural tests must check consequential outcomes. Analytical evaluation must examine whether the system's judgments are supported by evidence.
The checks also have to run. A build that merely strips TypeScript syntax does not enforce these obligations, and unchecked assertions or any can bypass them.
The useful payoff is specific: one class of omission no longer depends on a reviewer remembering to look for it. That leaves review time for questions the compiler cannot answer.
The next time a bug ends with “we forgot to update the other place,” inspect the relationship between those places. It may be an opportunity to replace a recurring reminder with an enforceable contract.