no-throw
Disallow throw statements.
Targeted pattern scopeâ
This rule targets ThrowStatement nodes.
What this rule reportsâ
- Any direct throw statement
Why this rule existsâ
Value-level error handling (for example Result/Either patterns) can be easier to model and compose.
â Incorrectâ
throw new Error("fail");
â Correctâ
return { ok: false, error: "fail" };
Additional examplesâ
// â Throwing during parsing
const parsePort = (value: string) => {
const port = Number(value);
if (!Number.isInteger(port) || port <= 0) {
throw new Error("Invalid port");
}
return port;
};
// â
Returning explicit parse outcome
const parsePort = (value: string) => {
const port = Number(value);
if (!Number.isInteger(port) || port <= 0) {
return { ok: false, error: "Invalid port" };
}
return { ok: true, value: port };
};
ESLint flat config exampleâ
import immutable from "eslint-plugin-immutable-2";
export default [
{
files: ["**/*.{js,mjs,cjs,ts,mts,cts,tsx}"],
plugins: { immutable },
rules: {
"immutable/no-throw": "error",
},
},
];
When not to use itâ
If your runtime model depends on exceptions (for example framework error boundaries, startup-fail-fast behavior, or assertion libraries), enforcing no-throw everywhere can be counterproductive. Prefer restricting this rule to business logic layers where value-based error handling is a goal.
Rule catalog ID: R911