Skip to content

unreachable-code ​

Code that can never run: statements after a call that never returns, and a branch the subject's closed type already decides. On by default, as an error. One file and the functions it imports say it all: reported in the editor and by safe-ts check alike.

Why: an agent that adds a case above the fallback, or a panic before the old code, leaves code that reads as if it ran. A fallback after every member is handled is worse than dead: it is what a member added to the type later falls into, silently, instead of failing to compile. TypeScript knows both (it greys the code out in an editor) but reports neither unless allowUnreachableCode is false, and never after an if run that handles every member.

✅ Accepted: the last branch says nothing is left, and TypeScript checks it

✓ Accepted0 diagnostics
ts
import { unreachable } from "@evobug_com/safe-ts-std/enum";

type Kind = "a" | "b";

export function label(kind: Kind): string {
  if (kind === "a") return "A";
  if (kind === "b") return "B";
  return unreachable(kind);
}

❌ Rejected: every value of kind is handled above the last line

✕ Rejectedunreachable-code
ts
type Kind = "a" | "b";

export function label(kind: Kind): string {
  if (kind === "a") return "A";
  if (kind === "b") return "B";
  return "?";
}

❌ Rejected: typeof took every kind x can be

✕ Rejectedunreachable-code
ts
export function describe(x: string | number): string {
  if (typeof x === "string") return x;
  if (typeof x === "number") return x.toFixed(2);
  return "neither";
}

❌ Rejected: code after a call that never returns

✕ Rejectedunreachable-code
ts
import { panic } from "@evobug_com/safe-ts-std/result";

export function ratio(done: number, total: number): number {
  if (total === 0) {
    panic("no work");
    console.log("ratio of nothing");
  }
  return done / total;
}

What it reports ​

  • After a call that never returns: std's panic(...) and unreachable(x), process.exit(), a function of the file declared to return never, and an imported function the program declares so (function fail(message: string): never, imported by name or as the module's default; the return type may be an alias of the file, type Fails = never). The function's control-flow graph (flow) is walked as written and with those calls ending their path (an exception they throw still reaches a catch); what only the first walk reaches is reported, one finding per run of statements. Code after a return, throw, break or continue is oxlint's no-unreachable, which the SAFE-TS preset makes an error, and not reported again; so are if (false) (no-constant-condition), a case written twice (no-duplicate-case) and an else if repeating its chain (no-dupe-else-if), errors in the preset too. The preset names them, so a project that sets oxlint's categories itself keeps them, and turning one off in .oxlintrc.json is named with the settings that check less.
  • A branch the closed type decides (the same reading of the subject's type as exhaustive-dispatch): the rest of an if run, an if / else if chain or a ?: chain after tests that handle every member. A test narrowed by && (kind === "a" && ready) handles only part of its member; a type with undefined or null in it isn't closed. A switch's default after every case is exhaustive-switch's, which asks for default: unreachable(x). A test of a member a test above already took is refused by TypeScript in an if run or a chain (the subject is narrowed: TS2367, "no overlap"), and a case repeating one, however it is spelled (Kind.A and "a"), is duplicate-code-value's.
  • A branch the type tests decide: the same, for tests of what a value is: typeof x === "string", x instanceof Cat (a class of the program; its subclasses too), x === undefined (== null), x === null, Array.isArray(x), "key" in x (on object types that always have the key). When the tests above take every kind the written type has (string | number, Cat | Dog, string | undefined, Circle | Square), TypeScript has narrowed the value to never there without saying so, and the rest can't run. A type the checker can't write out (unknown, a library's type, a type parameter) is never decided; neither is instanceof on an object type (an instance of anything, structurally), in on a class, nor === null on a type whose undefined may be a null.

On an adopter: none after the Option fix (three ?: and if runs over Option<"a" | "b"> were read as closed and reported, wrongly: the type has undefined too, and is no longer read as closed). The code had no statement after a call that never returns.

What it can't see ​

  • An imported never function in a branch: it is decided on the statements after the call in the same block, not on the graph (the whole-program pass doesn't have it), so if (x) fail(); else fail(); more(); with an imported fail isn't reported.
  • A function that never returns without saying so (a loop with no exit, a function that only throws but is declared void): declare it never.
  • Narrowing before the dispatch: the closed type is the declared one. An earlier if (kind === "a") return and a later run over "b" alone with a fallback isn't seen as decided.
  • A catch around code that can't throw. Not reported: the failure tracking is about what can propagate a PropagateError and what a pack says throws, not about every way code throws (a getter, a RangeError from a built-in the maps call safe for its arguments, a stack overflow), so "this can't throw" would be a guess.

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra