prefer-promise-reject-errors
Require using Error objects as Promise rejection reasons.
✅
Extending "plugin:@typescript-eslint/recommended-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
's options.
How to Use
- Flat Config
- Legacy Config
eslint.config.mjs
export default tseslint.config({
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"
}
});
.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 Troubleshooting > Linting with Type Information > Performance if you experience performance degradations after enabling type checked rules.