Make a missing registry entry a type error
A table keyed by every registered type, typed so that adding a new type without an entry fails the typecheck, and why two pull requests that each pass CI can still break main together.
Many codebases have a registry. It is a table with one entry for each kind of thing: each job type, each event, each plugin. The table is easy to forget. Someone adds a new kind and nobody adds the entry. The failure shows up at run time, in the one code path that looks the entry up. This post shows how to move that failure to the typecheck.
The union is the source of truth
Start with one type that lists every kind.
export type Kind = "email" | "sms" | "push";
Every other table should be tied to this type. A new kind then changes the type, and the compiler finds every table that needs a matching entry.
Record plus satisfies
A Record<Kind, T> is an object that must have exactly one key for each member of the union. The satisfies operator checks an object against that type without changing the type that TypeScript infers for the object.
export const labels = {
email: "Email",
sms: "Text message",
push: "Push notification",
} satisfies Record<Kind, string>;
I ran this with tsc --strict and it compiles with no output. Then I removed the push entry and ran it again. The compiler stopped with an error that names the missing key.
error TS2741: Property 'push' is missing in type '{ email: string; sms: string; }' but required in type 'Record<Kind, string>'.
That error is the whole point. Adding "webhook" to Kind makes every table like this one fail until someone writes the entry. Nobody has to remember anything.
Typing the variable with Record<Kind, string> as an annotation also catches a missing key. I use satisfies instead. It leaves the inferred type of the table alone, and I can read it at a glance as a check.
An extra key is also an error, so a typo in a key name does not slip through as a harmless extra entry. I tried that with a misspelled key and the compiler rejected it too.
The failure mode I wanted to cover
A table without this typing fails late. Here is the version that hurts.
export const labels: Record<string, string> = {
email: "Email",
sms: "Text message",
};
A string key accepts any set of entries. labels["push"] has type string and may be undefined at run time. The type says it cannot be, so the compiler stays quiet until a user hits that path.
Why separate green pull requests can break main
The typed table has a second job. It catches a failure that no single pull request can show.
Take two pull requests that both start from the same main.
- Pull request one adds
"webhook"toKindand adds the matching entry to the existinglabelstable. - Pull request two adds a new table,
icons, typed withsatisfies Record<Kind, string>. It lists the three kinds that exist on its base.
Each pull request passes its own CI. Pull request one has its entry, and pull request two covers every kind that its base knows. They touch different lines, so git sees no conflict. After both merge, Kind has four members and icons has three keys. Main fails the typecheck.
The typed table is what turns this into a loud failure. Without it, main would be broken only at run time, and nobody would know until a user reached the missing entry. The typecheck on main would still be green.
CI checks each pull request against the base it had when the run started. It never checks the combination of two pull requests that have not merged yet.
Closing the gap
A typed registry finds the combination problem after the merge. These settings find it before.
- Require branches to be up to date before merging, so a pull request is re-tested on top of the latest main.
- Use a merge queue. GitHub's workflow events include
enqueuedanddequeuedfor pull requests, which exist for this purpose. A queue tests each change on top of the ones ahead of it. - Run the typecheck on main after every merge, and treat a red main as the top priority.
The first two cost waiting time. The third is cheap, and it is the one I would never skip.
When to use the pattern
Use it when a table must have one entry for every member of a set that grows. Handlers by event name, labels by status, icons by kind, permissions by role and defaults by environment all fit.
Do not use it when missing entries are fine. A cache keyed by user id has no reason to cover all users. A Partial<Record<Kind, string>> says that honestly, and then the lookup type includes undefined and the compiler makes you handle it.