Skip to main content

prefer-promise-reject-errors

Require using Error objects as Promise rejection reasons.

💭

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

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 Playground

How to Use

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"
}
});

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.

Resources

Taken with ❤️ from ESLint core.