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

Promise.allSettled type problem #49344

Closed
dogukanakkaya opened this issue Jun 1, 2022 · 4 comments
Closed

Promise.allSettled type problem #49344

dogukanakkaya opened this issue Jun 1, 2022 · 4 comments

Comments

@dogukanakkaya
Copy link

dogukanakkaya commented Jun 1, 2022

This code gives me some null errors even though i filtered the array. Type 'number | null' is not assignable to type 'number'.

promiseResults.map(promiseResult => promiseResult.status === 'fulfilled' ? promiseResult.value : null).filter(Boolean)

And also below code gives me Property 'value' does not exist on type 'PromiseRejectedResult'. even though i filtered for fulfilled promise results.

promiseResults.filter(promiseResult => promiseResult.status === 'fulfilled').map(promiseResult => promiseResult.value)
@fatcerberus
Copy link

Neither Boolean nor txData => txData.status === 'fulfilled' are type guards. You need to pass a type guard (a function with an explicit param is T return type annotation) for filter to narrow the type of the array.

@SPGoding
Copy link

SPGoding commented Jun 2, 2022

See also #16655 for .filter(Boolean).

@dogukanakkaya
Copy link
Author

I already have some workaround for those, but i am still not convinced that this is not an issue (especially for filter if not for Promise Result types)? Shouldn't Typescript compiler already infers the types for filtering with var !== null.

Some of those workarounds for filter are below code and i can make it work also with as PromiseFulfilledResult for Promise thing.

const numbers = [1, 2, 3, 4, null];
numbers.filter((num): num is number => num !== null);

@MartinJohns
Copy link
Contributor

Shouldn't Typescript compiler already infers the types for filtering with var !== null.

See #38390.

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

No branches or pull requests

4 participants