An error is created and dropped instead of thrown

tx0/ts:unthrown-errordefaultwarningbug

What it finds

new Error("negative"); on a line of its own: the error is created and dropped, and nothing is thrown. throw was left out.

The class must be one of the global error classes, such as Error or TypeError, called with or without new, or a class of this file, without decorators, that extends one, directly or through other such classes, created with new. Names the file imports or declares in any other way are left alone.

From the rule's source, unthrown_error.rs.

Example from the rule's tests
import { ImportedError } from "./errors"; class AppError extends Error {}class NotFound extends AppError {}class Plain {} function check(n: number, log: (e: unknown) => void) {  if (n < 0) {    new Error("negative");tx0 reports: this Error is created but not thrown  }  if (n === 0) {    new NotFound();tx0 reports: this NotFound is created but not thrown  }  if (n === 1) {    TypeError("no new");tx0 reports: this TypeError is created but not thrown  }  if (n === 2) {    throw new RangeError("thrown");  }  const error = new Error("kept");  log(new Error("passed on"));  new ImportedError();  new Plain();  return error;} function shadowed(Error: new () => object) {  new Error();}
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:unthrown-error" = "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:unthrown-error] reason="..."

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