-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Incorrect type inference of Promise.all when spreading Promise arrays #40330
Comments
I have this issue as well. It happens in the following code: declare function a(): Promise<number>
declare function b(): Promise<string>
async function main() {
const x = await Promise.all(
[
...[2].map(() => a()),
b()
]
)
} Could variadic tuples maybe tackle this problem? |
I opened this same issue: #43157 |
Is my error related to this issue?
Note: All those With a single promise, it's all fine. But as soon as I pass more than one promise to the array, it shows the following error: Full error content:
What could this be? |
Im just wondering when this gets fixed? My code cant use generic type argument of Promise.al, else type of a result 'a' can then become any of them which is not the case :) So code below is not working: const [a, b, c] = await Promise.all([
api.getFoo(locale),
api.getBar(locale),
...(searchTerm
? [api.searchProducts(locale, searchTerm)]
: [null]),
]); version: 4.3.5 |
Seems to be resolved now on 4.6.4 |
You can use it this way, I tested it and it works const promises: Promise<number, string>[] = [promiseNumber, promiseString];
const res = await Promise.all(promises); |
You dont want |
@maapteh Sorry for this, i tried it with types |
TypeScript Version: 4.0.2 (but also the version in the Playground)
Search Terms:
Promise.all type inference
Code
Expected behavior:
The Promise.all call should not throw any compile time errors due to type mismatches, since Promise.all simply returns the values of the array and waits until resolution if the value is a promise.
Actual behavior:
A compile time error is thrown:
Workaround:
Setting the generic type argument of Promise.all to
void | number
works, but this is a step backwards after TypeScript added better Promise.all inference and it should know that the first argument that will be returned from the call will be a number:Playground Link:
https://www.typescriptlang.org/play?#code/IYZwngdgxgBAZgV2gFwJYHsI2QUxMgCgEoYBvAKHJmpik3xgAcAndAW1RBwDkE2AjHMxgBeGAAVWHLgDpmedABsAbjgIBGIgG4qNOhAYt2nHADV0qACaiYoSLGKiAfGQC+lGrYDuwVMglSJjLAiooEANq6ntRG0jx8gswANFHRMunhsSbmVsQAuql5ROSuQA
Related Issues:
The text was updated successfully, but these errors were encountered: