Dev Helpers
← Back

Read an error-only field

Read an error-only field 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(
  ((s: State) => (s.phase === "error" ? s.reason : null))({
    phase: "error",
    reason: "timeout",
  }),
);

Sample output

timeout