You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here's another example I have of Promise.all behavior changes in 3.7.2 vs 3.6. I tried testing it with the new declarations in #33707 but it has the same error result.
/* eslint-disable */typeAwaited<T>=Textendsundefined ? T : TextendsPromiseLike<infer U> ? U : T;functionall<Textendsreadonlyany[]>(values: T): Promise<{-readonly[PinkeyofT]: Awaited<T[P]>}>{returnPromise.all(values)asany;}classK{getA=(): Promise<string|undefined>=>{returnPromise.resolve(undefined);};getB=(): Promise<string>=>{returnPromise.resolve('');};getC(){// working in 3.6.3, error in 3.7.2Promise.all([this.getA(),this.getB()]).then(([a,b])=>{consta_: string|undefined=a;// ts2322, b is inferred as `string | undefined`constb_: string=b;return[a_,b_];});// attempt to test with new type in PR #33707, same resultall([this.getA(),this.getB()]).then(([a,b])=>{consta_: string|undefined=a;// ts2322, b is inferred as `string | undefined`constb_: string=b;return[a_,b_];});}}
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.
typeAwaited<T>=Textendsundefined ?
T :
TextendsPromiseLike<infer U> ?
U :
T;declarefunctionall<Textendsreadonlyany[]>(values: T): Promise<{-readonly[PinkeyofT]: Awaited<T[P]>}>;//for empty tupledeclarefunctionbetterAll(arr : []) : Promise<[]>;//for non-empty tupledeclarefunctionbetterAll<ArrTextendsreadonly[any, ...any[]]>(arr: ArrT): Promise<{-readonly[iinkeyofArrT]: Awaited<ArrT[i]>}>;//for non-tuple arraydeclarefunctionbetterAll<ArrTextendsreadonlyany[]>(arr: ArrT): Promise<{-readonly[iinkeyofArrT]: Awaited<ArrT[i]>}>;//https:https://github.com/microsoft/TypeScript/issues/34925#issuecomment-550069150classK{getA=(): Promise<string|undefined>=>{returnPromise.resolve(undefined);};getB=(): Promise<string>=>{returnPromise.resolve('');};getC(){// working in 3.6.3, error in 3.7.2Promise.all([this.getA(),this.getB()]).then(([a,b])=>{consta_: string|undefined=a;// ts2322, b is inferred as `string | undefined`constb_: string=b;return[a_,b_];});// attempt to test with new type in PR #33707, same resultall([this.getA(),this.getB()]).then(([a,b])=>{consta_: string|undefined=a;// ts2322, b is inferred as `string | undefined`constb_: string=b;return[a_,b_];});betterAll([this.getA(),this.getB()]).then(([a,b])=>{consta_: string|undefined=a;//OK!constb_: string=b;return[a_,b_];});}}
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.all typings are broken in lots of projects in 3.7.3.
Reacted by Toni Villena, Joseph Kohlmann, Jan Potoms, Derek Petersen, ovstackelberg, fabifink, Tarek Haoula, Robert Böhm, Manuel Vögele, Glen Cheney and 21 more
I'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.
Activity
nmain commentedon Nov 5, 2019
Possible duplicate of #34924
AnyhowStep commentedon Nov 5, 2019
Not a duplicate. Also shown that this PR, as it is, will not fix this issue.
#33707
#33707 (comment)
stevehollaar commentedon Nov 5, 2019
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+6gH4qkA
AnyhowStep commentedon Nov 5, 2019
Steve Hollaar (@stevehollaar)
#33707 will fix that example.
Playground
junoatwork commentedon Nov 5, 2019
Here's another example I have of Promise.all behavior changes in 3.7.2 vs 3.6. I tried testing it with the new declarations in #33707 but it has the same error result.
AnyhowStep commentedon Nov 5, 2019
I'm on mobile, so I can't try it but can you try the
betterAlldeclaration?thaoula commentedon Nov 6, 2019
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.
AnyhowStep commentedon Nov 6, 2019
junoatscroll
Playground
The
betterAll()type works for your use caseAnyhowStep commentedon Nov 6, 2019
Tarek Haoula (@thaoula)
Playground
RyanCavanaugh commentedon Nov 8, 2019
Ron Buckton (@rbuckton) can you investigate? Thanks!
strelga commentedon Dec 11, 2019
Hey, Ryan Cavanaugh (@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.strelga commentedon Dec 20, 2019
Ryan Cavanaugh (@RyanCavanaugh) Ron Buckton (@rbuckton) friendly ping, just in case you missed my message
csojinb commentedon Jan 2, 2020
I'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:
schusovskoy commentedon Jan 21, 2020
I've also found that it can be related with how readonly modifier works. Here is the playground link describing what I mean.
adrienboulle commentedon Feb 9, 2020
Ryan Cavanaugh (@RyanCavanaugh) if it can help i have this example
Here the thing that break type inferring is (oddly) the type
{[key: string]: any}moodseller commentedon Feb 18, 2020
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.