Incorrect type inference of Promise.all when spreading Promise arrays
See original GitHub issueTypeScript 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:
Related Issues:
Issue Analytics
- State:
- Created 3 years ago
- Reactions:8
- Comments:8 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@maapteh Sorry for this, i tried it with types
<string, number>
. Yah, it seems to be fixed, so i solved my problem this way 😄You dont want
<number, void>
, what you want is that types come from their own.But it seems to be fixed 😃