Skip to content
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

Typing of toPromise is wrong #4196

Closed
plol opened this issue Sep 27, 2018 · 3 comments
Closed

Typing of toPromise is wrong #4196

plol opened this issue Sep 27, 2018 · 3 comments
Labels
TS Issues and PRs related purely to TypeScript issues

Comments

@plol
Copy link

plol commented Sep 27, 2018

Bug Report

Current Behavior
toPromise has the wrong type

Reproduction

const p = of<string>().toPromise() // Type is Promise<string>

Expected behavior
p will in fact resolve to undefined and should have the type Promise<string | undefined>

@cartant
Copy link
Collaborator

cartant commented Sep 27, 2018

This is a fairly subtle problem.

The promise will resolve when the observable completes - resolving with the last emitted value or with undefined if a value wasn't emitted.

However, the type declarations cannot express whether or not an observable will emit a value at runtime, so it could be argued that the type for the promise should always be Promise<T | undefined>.

That would more correctly describe the behaviour, but it would be a breaking change.

@cartant
Copy link
Collaborator

cartant commented Sep 27, 2018

Perhaps a solution worth considering - for v7 - would be to allow a fallback value to be passed to toPromise.

The fallback would be used if the source completes without emitting and could default to undefined.

@cartant cartant added the TS Issues and PRs related purely to TypeScript issues label Sep 29, 2018
@benlesh
Copy link
Member

benlesh commented Sep 1, 2020

This was fixed in #5072

@benlesh benlesh closed this as completed Sep 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
TS Issues and PRs related purely to TypeScript issues
Projects
None yet
Development

No branches or pull requests

3 participants