Dev Helpers
← Back

Count phases in a state history

Count phases in a state history using TypeScript.

Open this example in the interactive library

Source code

type State =
  | { phase: "idle" }
  | { phase: "loading"; id: number }
  | { phase: "ready"; text: string }
  | { phase: "error"; reason: string };
console.log(
  ((states: State[]) => states.map((s) => s.phase).join(" → "))([
    { phase: "idle" },
    { phase: "loading", id: 2 },
    { phase: "ready", text: "ok" },
  ]),
);

Sample output

idle → loading → ready