TypeScript Discriminated Unions: Make Illegal States Unrepresentable
A few weeks ago I was reviewing a pull request where a component crashed in production with the classic Cannot read properties of undefined. The bug wasn't a typo or a missing null check—it was something more subtle. The type allowed a combination of values that should never have existed in the first place. The data was loading: false, error: null, and data: undefined all at once, and nobody could tell me what that state was supposed to mean. That's the trap of modeling state with a bag of optio