Dev Helpers
← Back

Carry an existing state through a no-op

Carry an existing state through a no-op 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 extends State>(state: S): S => state)({ phase: "ready", text: "cached" }),
);

Sample output

{ phase: 'ready', text: 'cached' }