Skip to content

fix: #57 more generic types for Promise.then and Promise.catch #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 14, 2025

Conversation

danielrentz
Copy link
Contributor

Fixes #57

This PR contains the following changes:

  • Parameter onrejected of Promise.catch is now required. Calling promise.catch() is technically allowed in JS but rather useless (except for postponing the new promise to the next tick which can still be done with promise.then(null) if really needed).
  • Promise.catch is now generic and allows to add a new type to the resulting promise.

For consistency, Promise.then and PromiseLike.then have been adapted as well:

  • Parameter onfulfilled is now required (see above).
  • Parameter onrejected allows to return another type, like Promise.catch above.
  • Passing a function to onfulfilled changes the resulting promise to the new type (Promise<U>).
  • Passing a function or nullish to onfulfilled widens the resulting promise to the old and new type (Promise<T|U>). This may happen for example when passing a variable containing a callback or null.

One may think to go even further and to allow to return different types from onfulfilled and onrejected (effectively resulting in Promise<U|V> or even Promise<T|U|V> with nullish onfulfilled) but I think this is out of scope of this PR.

Copy link
Owner

@uhyo uhyo left a comment

Choose a reason for hiding this comment

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

Thank you for contribution!

The implementation looks correct and the tests are good too.

@uhyo uhyo merged commit 222baac into uhyo:master Jan 14, 2025
1 check passed
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.

Promise.catch should allow to widen promise type
2 participants