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 Promises 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);
});
Promise.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());
});
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 ↗
Options
See eslint/prefer-promise-reject-errors's options.
This rule adds the following options:
interface Options {
  /**
   * Whether to always allow throwing values typed as `any`.
   */
  allowThrowingAny?: boolean;
  /**
   * Whether to always allow throwing values typed as `unknown`.
   */
  allowThrowingUnknown?: boolean;
}
const defaultOptions: Options = {
  allowThrowingAny: false,
  allowThrowingUnknown: false,
};
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.