State Management · Senior · Question 73 of 75
Why model async state as a discriminated union instead of separate fields?
type State =
| { status: "idle" }
| { status: "loading" }
| { status: "success"; data: User[] }
| { status: "error"; error: string };- ✓Correct answer: Impossible combinations (loading with data and error) can't be represented, and TypeScript narrows
dataper status - 2It uses less memory
- 3Reducers require it
Explanation
Libraries like TanStack Query expose the same idea via status. In your own reducers it removes defensive checks throughout the UI.