fix: #57 more generic types for Promise.then
and Promise.catch
#60
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #57
This PR contains the following changes:
onrejected
ofPromise.catch
is now required. Callingpromise.catch()
is technically allowed in JS but rather useless (except for postponing the new promise to the next tick which can still be done withpromise.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
andPromiseLike.then
have been adapted as well:onfulfilled
is now required (see above).onrejected
allows to return another type, likePromise.catch
above.onfulfilled
changes the resulting promise to the new type (Promise<U>
).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
andonrejected
(effectively resulting inPromise<U|V>
or evenPromise<T|U|V>
with nullishonfulfilled
) but I think this is out of scope of this PR.