TypeScript: Promise.all not correctly inferring types.
TypeScript Version: 3.7.2, v3.8.0-dev.20191102
Search Terms: Promise.all Wrong type
Code
const f1 = async (): Promise<string> => {
return '';
};
const f2 = async (): Promise<number> => {
return 0;
};
const f3 = async (): Promise<Object> => {
return {};
};
const res = Promise.all([f1(), f2(), f3()]);
Expected behavior:
res has type Promise<[string, number, Object]>.
Actual behavior:
res has type Promise<Object[]>.
Related Issues:
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 80
- Comments: 16 (6 by maintainers)
Hey, @RyanCavanaugh!
Is there any chance a fix of this or related (#34937, #35258, etc) issues could be squeezed into some of the next 3.7.x releases? Seems like
Promise.alltypings are broken in lots of projects in 3.7.3.I have another Playground link example of
Promise.allinferring types incorrectly. It seems when one of the results is nullable, TS incorrectly makes all results nullable: http://www.typescriptlang.org/play/?ssl=8&ssc=1&pln=8&pc=5#code/MYewdgzgLgBAZgUysAFgRhgXhgCgJQBcMACgE4gC2AlhAgDxgCuFARgqTAD4xMA2vAPixCylGggB0pBBBC8Abghxo8AbgBQoSLETIUAJiy5CJctVp1opKmADmAbQC6QzCLPipMuYpz2A5ACGfgA0MH4sfo5q6prg0DABRgEQAJ5gwMbCMADe6jAwWvH2pGihpPqORPZMrOycfLyhVjYOjo5JAO4BVLCi5pIB-L66qGj4oSMG+FEa+YVykrwgtjglweV46gC+6gH4qkAI’ve got another example here. From my tests, it looks like this occurs whenever one of the promises has a narrower type that is assignable to the type of another promise in the list. I’ve only been able to get around it by explicitly typing
Promise.all.Note that I’m seeing this type widening occur on earlier versions of TS than 3.7, at least in the playground.
Copying code from the playground here too:
@stevehollaar
#33707 will fix that example.
Playground
I have also found that using
Promise.allin such way:const [pr1, pr2] = await Promise.all([fn1(), fn2()]);resolves
pr1, pr2variable types totype | nullif any of the async functions which are present inPromise.allcan alternatively returnnull.@thaoula
Playground
We have quite a few Promise.all and array destructuring statements in our codebase. Most are working like the Typescript 3.6.3 version except any statement that involves promises returning any.
Whenever any promise in the Promise.all call returns any all the destructured variables are marked as any.
@RyanCavanaugh @rbuckton friendly ping, just in case you missed my message