Menu

Post image 1
Post image 2
Post image 3
1 / 3
0

Branded Types in TypeScript: Cheap Type Safety You Should Already Be Using

DEV Community·Gabriel Anhaia·28 days ago
#6jKWN7lb
Reading 0:00
15s threshold

Book: The TypeScript Type System — From Generics to DSL-Level Types Also by me: The TypeScript Library — the 5-book collection My project: Hermes IDE | GitHub — an IDE for developers who ship with Claude Code and other AI coding tools Me: xgabriel.com | GitHub A function takes a userId: string and an orderId: string . During a refactor, two arguments cross. The new call reads refund(orderId, userId) instead of refund(userId, orderId) . The checker shrugs. Both are strings. Both pass. The unit test for refund ran against a happy-path fixture where the IDs happened to be different lengths, so the SQL still returned a row. Just the wrong one. A customer reads someone else's order history before the on-call notices. This bug is older than TypeScript. Languages with nominal type systems can declare a UserId distinct from every other string, and the compiler refuses to confuse them. TypeScript is structural. A string is a string . Two type aliases that resolve to the same primitive are the same type.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More