Skip to content

Loosen @typescript-eslint/prefer-promise-reject-errors rule to allow rejecting with unknown#389

Merged
Mrtenz merged 3 commits intomainfrom
mrtenz/disable-@typescript-eslint/prefer-promise-reject-errors
Mar 24, 2025
Merged

Loosen @typescript-eslint/prefer-promise-reject-errors rule to allow rejecting with unknown#389
Mrtenz merged 3 commits intomainfrom
mrtenz/disable-@typescript-eslint/prefer-promise-reject-errors

Conversation

@Mrtenz
Copy link
Copy Markdown
Member

@Mrtenz Mrtenz commented Feb 19, 2025

This changes the @typescript-eslint/prefer-promise-reject-errors rule, which enforces that Promise.reject must be called with an instance of Error. After this change, reject may also be called with unknown values. For example, the following code was previously invalid, but is valid after this change:

new Promise((resolve, reject) => {
  try {
    return resolve(doSomething());
  } catch (error) {
    // `error` is of type `unknown`.
    return reject(error);
  }
});

@Mrtenz Mrtenz marked this pull request as ready for review February 19, 2025 13:13
@Mrtenz Mrtenz requested review from a team as code owners February 19, 2025 13:13
@Gudahtt
Copy link
Copy Markdown
Member

Gudahtt commented Feb 19, 2025

To clarify, are you saying that it's too strict because we'd be forced to verify that the error is an instance of Error?

Surely we're not intentionally throwing non-Errors. That's what I saw as the benefit of this rule - it's an additional check to ensure we're not throwing a non-Error.

If handling unknown is the only concern, there is an option for that: https://typescript-eslint.io/rules/prefer-promise-reject-errors/

@Mrtenz
Copy link
Copy Markdown
Member Author

Mrtenz commented Feb 19, 2025

To clarify, are you saying that it's too strict because we'd be forced to verify that the error is an instance of Error?

Yes. It was causing a lot of errors in Snaps at least.

Surely we're not intentionally throwing non-Errors. That's what I saw as the benefit of this rule - it's an additional check to ensure we're not throwing a non-Error.

If handling unknown is the only concern, there is an option for that: https://typescript-eslint.io/rules/prefer-promise-reject-errors/

Good point. I wasn't aware of that option, that seems like a good alternative to completely disabling the rule.

@Mrtenz Mrtenz force-pushed the mrtenz/disable-@typescript-eslint/prefer-promise-reject-errors branch from 3f087a4 to 330e753 Compare March 1, 2025 11:05
@Mrtenz Mrtenz changed the title Disable @typescript-eslint/prefer-promise-reject-errors rule Loosen @typescript-eslint/prefer-promise-reject-errors rule to allow rejecting with unknown Mar 1, 2025
@Mrtenz Mrtenz force-pushed the mrtenz/disable-@typescript-eslint/prefer-promise-reject-errors branch from 330e753 to e692b25 Compare March 1, 2025 11:07
Copy link
Copy Markdown
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense!

@Mrtenz Mrtenz merged commit 44d9c78 into main Mar 24, 2025
22 checks passed
@Mrtenz Mrtenz deleted the mrtenz/disable-@typescript-eslint/prefer-promise-reject-errors branch March 24, 2025 21:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants