Discriminated unions for state

Back to list2025-10-29
You

How do I model loading and error states without booleans?

ChatGPT

A discriminated union removes impossible states:

type State =
  | { status: "idle" }
  | { status: "loading" }
  | { status: "ok"; data: User[] }
  | { status: "error"; message: string };

With three booleans you can represent loading and error simultaneously; with this you cannot, and the compiler enforces handling each case.

You

What would you skip entirely?

ChatGPT

The reporting layer, until something forces it. It is the part that feels productive to build and the part nobody opens twice.