throw-error
Disallow throwing or rejecting values that are not Error-like.
⚠️ This rule requires type information to run.
Targeted pattern scope
This rule checks:
ThrowStatementarguments,Promise.reject(...)calls,reject(...)calls insidenew Promise((resolve, reject) => ...)executors.
What this rule reports
This rule reports values that are not Error-like when thrown or used as Promise rejection values.
It allows:
ErrorandDOMExceptionvalues,- values that could be
anyorunknown(to avoid unsafe assumptions during static analysis).
Why this rule exists
Throwing primitives or arbitrary objects makes error handling inconsistent and often forces fragile downstream narrowing logic. Enforcing Error-like values improves reliability and observability.
❌ Incorrect
throw "kaboom";
Promise.reject("kaboom");
new Promise((resolve, reject) => reject("kaboom"));
✅ Correct
throw new Error("kaboom");
throw new DOMException("kaboom");
Promise.reject(new Error("kaboom"));
new Promise((resolve, reject) => reject(new Error("kaboom")));
Behavior and migration notes
This rule has no options.
Additional examples
declare const maybeUnknown: unknown;
throw maybeUnknown;
// ✅ allowed (unknown is intentionally permitted)
const payload = { code: "E_FAIL" };
throw payload;
// ❌ reported (not Error-like)
ESLint flat config example
import etcMisc from "eslint-plugin-etc-misc";
export default [
{
plugins: { "etc-misc": etcMisc },
rules: {
"etc-misc/throw-error": "error",
},
},
];
When not to use it
Disable this rule if your codebase intentionally uses non-Error throw/reject values and has established handling utilities for that pattern.
Package documentation
Rule catalog ID: R074
Further reading
Adoption resources
- Start at warning level in CI, then move to error after cleanup.
- Use focused codemods/autofix batches per package or directory.