Represent async UI states safely.

Section: API and Domain Modeling Recipes

State machine union

typescript
typescript
type AsyncState<T> =
  | { status: "idle" }
  | { status: "loading" }
  | { status: "success"; data: T }
  | { status: "error"; error: string };
Explanation

Discriminated unions make UI rendering branches easier to reason about.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing API and Domain Modeling Recipes
Paginated API response type
Model paginated collections with generics.
OpenIn sheettypescriptsame section
Form field error map
Type validation errors by field name.
OpenIn sheettypescriptsame section
Typed event map
Model event payloads by event name.
OpenIn sheettypescriptsame section
Use `NonNullable<T>`
Remove `null` and `undefined` from a type.
OpenIn sheettypescript1 tag match
Use `Extract<T, U>`
Keep only members assignable to another type.
OpenIn sheettypescript1 tag match
Use `Exclude<T, U>`
Remove members assignable to another type.
OpenIn sheettypescript1 tag match