question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incorrect type inference of Promise.all when spreading Promise arrays

See original GitHub issue

TypeScript Version: 4.0.2 (but also the version in the Playground)

Search Terms:

Promise.all type inference

Code

async function test() {

    const promiseNumber = Promise.resolve(1);
    const promiseVoid = async () => {}

    await Promise.all([
        promiseNumber,
        ...[promiseVoid()]
    ])
}

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:

No overload matches this call.
  The last overload gave the following error.
    Argument of type '(Promise<number> | Promise<void>)[]' is not assignable to parameter of type 'Iterable<number | PromiseLike<number>>'.
      The types returned by '[Symbol.iterator]().next(...)' are incompatible between these types.
        Type 'IteratorResult<Promise<number> | Promise<void>, any>' is not assignable to type 'IteratorResult<number | PromiseLike<number>, any>'.
          Type 'IteratorYieldResult<Promise<number> | Promise<void>>' is not assignable to type 'IteratorResult<number | PromiseLike<number>, any>'.
            Type 'IteratorYieldResult<Promise<number> | Promise<void>>' is not assignable to type 'IteratorYieldResult<number | PromiseLike<number>>'.
              Type 'Promise<number> | Promise<void>' is not assignable to type 'number | PromiseLike<number>'.
                Type 'Promise<void>' is not assignable to type 'number | PromiseLike<number>'.
                  Type 'Promise<void>' is not assignable to type 'PromiseLike<number>'.
                    Types of property 'then' are incompatible.
                      Type '<TResult1 = void, TResult2 = never>(onfulfilled?: ((value: void) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined) => Promise<...>' is not assignable to type '<TResult1 = number, TResult2 = never>(onfulfilled?: ((value: number) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<...>) | null | undefined) => PromiseLike<...>'.
                        Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible.
                          Types of parameters 'value' and 'value' are incompatible.
                            Type 'void' is not assignable to type 'number'.(2769)

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:

// Workaround which throws no errors:
async function test() {

    const promiseNumber = Promise.resolve(1);
    const promiseVoid = async () => {}

    await Promise.all<number | void>([
        promiseNumber,
        ...[promiseVoid()]
    ])
}

Playground Link:

https://www.typescriptlang.org/play?#code/IYZwngdgxgBAZgV2gFwJYHsI2QUxMgCgEoYBvAKHJmpik3xgAcAndAW1RBwDkE2AjHMxgBeGAAVWHLgDpmedABsAbjgIBGIgG4qNOhAYt2nHADV0qACaiYoSLGKiAfGQC+lGrYDuwVMglSJjLAiooEANq6ntRG0jx8gswANFHRMunhsSbmVsQAuql5ROSuQA

Related Issues:

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:8
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
tranminhwangcommented, May 19, 2022

@maapteh Sorry for this, i tried it with types <string, number>. Yah, it seems to be fixed, so i solved my problem this way 😄

1reaction
maaptehcommented, Oct 5, 2022

You dont want <number, void>, what you want is that types come from their own. But it seems to be fixed 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript: Incorrect type inference in Promise.all
Because the array passed to Promise.all isn't being typed explicitly, it's inferred to be a union of all its value types:
Read more >
Aggregate Results from Multiple Promises - JavaScript Tutorial
In this tutorial, you will learn how to use the Promise.all() method to aggregate results from multiple asynchronous operations.
Read more >
The starting point for learning TypeScript
Learn how to write declaration files to describe existing JavaScript. Important for DefinitelyTyped contributions. Introduction · Declaration Reference ...
Read more >
Crazy, Powerful TypeScript Tuple Types | by Eamonn Boyle
A tuple object has a fixed size, and the type of each element is ... The Promise.all method takes an array of promises...
Read more >
Promise.all() - JavaScript - MDN Web Docs
This returned promise fulfills when all of the input's promises fulfill (including when an empty iterable is passed), with an array of the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found