TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Promise.all returns Promise<Promise<T>[]>

Open msklvsk opened this issue 10 years ago • 12 comments

TypeScript Version:

1.8.9

Code

Promise.all([Promise.resolve('')]);  // has type Promise<Promise<string>[]>

Expected behavior: The expression has type Promise<string[]> as in 1.8.7 Actual behavior: The expression has type Promise<Promise<string>[]>

msklvsk avatar Mar 17 '16 18:03 msklvsk

+1 This is a regression in 1.8.9. Our build pipeline has a dep. on TS ~1.8.0. When 1.8.9 was fetched we started getting type errors in code like this:

let [{ content: x }, { content: y }] = Promise.all([fetch('x'), fetch('y')]);
// error TS2459: Type 'Promise<HttpResponseMessage>' has no property 'content' and no string index signature

jods4 avatar Mar 18 '16 09:03 jods4

I have a reproducible example here: https://gist.github.com/MarcusNoble/522b1fea7077be0af59a

MarcusNoble avatar Mar 18 '16 09:03 MarcusNoble

This is caused by 1589e4f57e39aa6c5bb9e0be39f60191cf94f8b9 limiting the depth of type inference to 5. There is no way of overriding this behaviour.

@vladima Are you able to weigh in on this?

MarcusNoble avatar Mar 19 '16 10:03 MarcusNoble

yes, I can repro this. To give some more context - originally depth limit was set to fix #7081. Value of depth was somewhat arbitrary - all large codebases that we have for tests worked fine with depth = 3 so with depth=5 we have some safety net. Apparently it was not enough. I propose to set the depth to 10 (current example starts working with depth=6). It still give a reasonable time on the test case from the original bug, I'll post time measurements later today, need to find them in mail.

// cc: @mhegazy

vladima avatar Mar 19 '16 16:03 vladima

I don't fully understand what happens under the hood here, but is this error message the one you'd expect? Shouldn't we have some kind of "type inference failed" error (or at least warning) for cases that go beyond whatever depth you set?

It is a bit surprising and unhelpful that TS decides the expression is Promise<T>[] when it is T[] without a warning. A message about failing to infer would have led me to the workaround of specifying types explicitly.

jods4 avatar Mar 19 '16 18:03 jods4

A bug fix release as 1.8.10 would be nice.

donaldpipowitch avatar Apr 11 '16 12:04 donaldpipowitch

Has this made 1.8.10?

jpsfs avatar Apr 18 '16 09:04 jpsfs

still reproduce at 1.8.10 version

    getResults(): Promise<string[]> {
        const results: string[] = [];
        const promises = results.map(result => {
            return Promise.resolve(result);
        });

        return Promise.all(promises);
    }
error TS2322: Type 'Promise<Promise<string>[]>' is not assignable to type 'Promise<string[]>'.
  Type 'Promise<string>[]' is not assignable to type 'string[]'.
    Type 'Promise<string>' is not assignable to type 'string'.

hbakhtiyor avatar May 09 '16 09:05 hbakhtiyor

The fix is in TS 2.0 as noted from the "Milestone".

mhegazy avatar May 09 '16 19:05 mhegazy

Any workaround until 2.0 is released?

jppellerin avatar May 16 '16 19:05 jppellerin

Revert to typescript 1.8.7 is what I had to do

tht13 avatar May 16 '16 19:05 tht13

Just confirm that the bug still can be reproduced in 1.8.10. What I did is that set the TAll explicitly.

promise all then

ZXS66 avatar Jul 27 '16 11:07 ZXS66