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(...)andunreachable(x),process.exit(), a function of the file declared to returnnever, 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 acatch); what only the first walk reaches is reported, one finding per run of statements. Code after areturn,throw,breakorcontinueis oxlint'sno-unreachable, which the SAFE-TS preset makes an error, and not reported again; so areif (false)(no-constant-condition), acasewritten twice (no-duplicate-case) and anelse ifrepeating 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.jsonis 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 anifrun, anif/else ifchain or a?:chain after tests that handle every member. A test narrowed by&&(kind === "a" && ready) handles only part of its member; a type withundefinedornullin it isn't closed. Aswitch'sdefaultafter everycaseisexhaustive-switch's, which asks fordefault: unreachable(x). A test of a member a test above already took is refused by TypeScript in anifrun or a chain (the subject is narrowed: TS2367, "no overlap"), and acaserepeating one, however it is spelled (Kind.Aand"a"), isduplicate-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 toneverthere 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 isinstanceofon an object type (an instance of anything, structurally),inon a class, nor=== nullon a type whoseundefinedmay be anull.
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
neverfunction 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), soif (x) fail(); else fail(); more();with an importedfailisn'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 itnever. - Narrowing before the dispatch: the closed type is the declared one. An earlier
if (kind === "a") returnand a later run over"b"alone with a fallback isn't seen as decided. - A
catcharound code that can't throw. Not reported: the failure tracking is about what can propagate aPropagateErrorand what a pack says throws, not about every way code throws (a getter, aRangeErrorfrom a built-in the maps call safe for its arguments, a stack overflow), so "this can't throw" would be a guess.