Sitemap

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.

6 min read2 days ago
Press enter or click to view image in full size

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) { /*…

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web
Already have an account? Sign in
Nexumo

Written by Nexumo

Next-move engineering for builders - fast, clear, measurable. Systems, data, and AI you can ship today.

Responses (3)

To respond to this story,
get the free Medium app.

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Branded types are a neat way to prevent accidental misuse of similar strings without adding runtime overhead.
Can you cover advanced scenarios next time? The basics are clear now.
Can this also be automated with CI/CD tools? Would be powerful.