Menu

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

Distributive Conditional Types: Why T extends X Splits on Unions

DEV Community·Gabriel Anhaia·29 days ago
#MzwycD9U
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 You write a helper that should answer a single yes-or-no question about a type. Is T a string, or is it not? You expect true or false . The compiler hands you back boolean . You stare at it for a minute, then realize T was a union, and TypeScript ran your conditional once per member and joined the results. That behavior has a name. Distributive conditional types. It is what makes Exclude work, what makes a careful IsString<T> answer true | false instead of boolean , and why library code wraps things in brackets when it needs to ask one question about a union as a whole. This post walks the rule, three places it earns its keep, and the one foot-gun you fix with brackets. The rule, in one sentence When T is a naked type parameter, T extends X ?…

Continue reading — create a free account

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

Read More