Design Patterns · Middle · Question 8 of 75
Why are isLoading, isError, isSuccess flags worse than an explicit state?
type State =
| { status: "idle" }
| { status: "loading" }
| { status: "success"; data: User }
| { status: "error"; error: string };- ✓Correct answer: Flags allow impossible combinations, while an explicit state allows only valid ones
- 2Flags take more memory than a single string
statusfield - 3TypeScript can't type several boolean fields at once
Explanation
Three boolean flags give 8 combinations, including meaningless ones like "loading and error at once". A discriminated union is essentially the State pattern / a finite state machine: transitions are explicit, and data is available only in success.