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 };

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.

Further reading

Problem with a question

Design Patterns · Middle · question 8

Test yourself

Take the Design Patterns · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz