Code that can never run

tx0/ts:unreachable-codedefaultwarningbug

What it finds

Statements after return, throw, break or continue in the same block never run.

Declarations that take effect before the code around them runs are not code that is skipped: function declarations, which are hoisted and often placed after the return they serve, var declarations without a value, and TypeScript's types, interfaces and ambient declarations. They are passed over, and the first statement after them that would have run is reported. Const enums are erased by the compiler and count as declarations too. Blocks the parser could not read completely are skipped, and so are statements it had to recover around, as when it reads a syntax it does not know.

From the rule's source, unreachable_code.rs.

Example from the rule's tests
function first(items: number[], log: (x: unknown) => void) {  for (const item of items) {    if (item) {      break;      log(item);tx0 reports: this code is never reached    }    continue;    log("next");tx0 reports: this code is never reached  }  return helper(items);  function helper(list: number[]) {    return list;  }  var later;  type Unused = string;  log("done");tx0 reports: this code is never reached  log("again");} function fail(message: string): never {  throw new Error(message);  return undefined as never;tx0 reports: this code is never reached} function guarded(items: number[]) {  if (items.length) {    return items;  }  return [];}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/ts:unreachable-code" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/ts:unreachable-code] reason="..."

tx0 explain tx0/ts:unreachable-code shows the same in the terminal. Found a false positive? Report it.