Menu

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

Stop Passing string for User IDs. Branded Types Aren't Optional Anymore

DEV Community·Gabriel Anhaia·about 1 month ago
#BlRKmTih
Reading 0:00
15s threshold

Book: The TypeScript Type System 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 bug ships on a Friday. The function signature reads refund(userId: string, orderId: string, amount: number) . The call site passes them in the wrong order. TypeScript compiles. The tests pass. Production discovers it Monday morning, after the support inbox has filled up. You have seen this bug. Every backend that handles money has a story like it, and the story always ends with somebody saying "how did the type system not catch this." The type system did exactly what it was asked. It checked that two strings were two strings. TypeScript is structurally typed by design. A UserId and an OrderId are both string underneath, so they are the same type to the compiler. That is the whole problem. The fix is four lines of code most teams still treat as advanced trivia.…

Continue reading — create a free account

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

Read More