Dev Helpers
← Back

Find the first recoverable error

Find the first recoverable error using TypeScript.

Open this example in the interactive library

Source code

type Result =
  { kind: "ok"; value: number } | { kind: "error"; message: string };
const results: Result[] = [
  { kind: "ok", value: 3 },
  { kind: "error", message: "offline" },
];
console.log(
  results.find(
    (r): r is Extract<Result, { kind: "error" }> => r.kind === "error",
  )?.message,
);

Sample output

offline