prefer-promise-reject-errors
Require using Error objects as Promise rejection reasons.
🔒
Extending "plugin:@typescript-eslint/strict-type-checked"
in an ESLint configuration enables this rule.
💭
This rule requires type information to run.
This rule extends the base eslint/prefer-promise-reject-errors
rule.
It uses type information to enforce that Promise
s are only rejected with Error
objects.
Examples
- ❌ Incorrect
- ✅ Correct
Promise.reject('error');
const err = new Error();
Promise.reject('an ' + err);
new Promise((resolve, reject) => reject('error'));
new Promise((resolve, reject) => {
const err = new Error();
reject('an ' + err);
});
Open in PlaygroundPromise.reject(new Error());
class CustomError extends Error {
// ...
}
Promise.reject(new CustomError());
new Promise((resolve, reject) => reject(new Error()));
new Promise((resolve, reject) => {
class CustomError extends Error {
// ...
}
return reject(new CustomError());
});
Open in PlaygroundOptions
See eslint/prefer-promise-reject-errors
options.
How to Use
.eslintrc.cjs
module.exports = {
"rules": {
// Note: you must disable the base rule as it can report incorrect errors
"prefer-promise-reject-errors": "off",
"@typescript-eslint/prefer-promise-reject-errors": "error"
}
};
Try this rule in the playground ↗
When Not To Use It
Type checked lint rules are more powerful than traditional lint rules, but also require configuring type checked linting. See Performance Troubleshooting if you experience performance degredations after enabling type checked rules.