Member-only story
12 TypeScript Type Patterns That Nuke Runtime Checks
Practical, copy-pasteable type tricks that move validation to compile time — so your production code stays thin, fast, and boring.
Twelve TypeScript type-level patterns — branded types, discriminated unions, satisfies, template literals, zod-free DTOs — that remove runtime checks with safe compile-time guarantees.
You might be wondering: can TypeScript actually delete code?
Not literally — but it can delete entire classes of runtime checks. The trick is to encode intent in the type system so the compiler refuses to compile bad states. Fewer ifs. Fewer guards. Better sleep.
Below are twelve battle-tested patterns I lean on in real projects. Each includes a short “when to use,” a snippet, and why it kills a runtime check.
1) Nominal/Branded Types (kill “wrong-string” bugs)
Use when: Two strings look the same but are not interchangeable (UserId vs OrderId).
type Brand<T, B extends string> = T & { readonly __brand: B };
type UserId = Brand<string, "UserId">;
type OrderId = Brand<string, "OrderId">;
declare const uid: UserId;
declare const oid: OrderId;
function getUser(u: UserId) { /*…