I have a problem with Promises in Typescript. I'm trying to call Promise.all with an array of promises but instead, I get
No overload matches this call. The last overload gave the following error. Argument of type '(Promise | Promise)[]' is not assignable to parameter of type 'Iterable<boolean | PromiseLike>'. [...]
Below a simplified example of I'm referring to:
const promise1 = Promise.resolve(true); //promise1:Promise<boolean>
const promise2 = Promise.resolve('promise...'); //promise2:Promise<string>
const promises = [promise1, promise2];
Promise.all(promises); //Error here
However, the code below works as I'd assume.:
const promise1 = Promise.resolve(true);
const promise2 = Promise.resolve('promise...');
Promise.all([promise1, promise2]);
Seems like a bug to me, but I'm not sure. I've been looking through GitHub, searching this problem but I didn't find any matching. Or am I doing something wrong and there is a solution so you can overcome the problem. Obviously, I can't just write Promise.all([promise1, promise2]) in my primary code because the array of promises is dynamic.
I'm using typescript @3.9.6.
const promises: Promise<boolean|string>[] = [promise1, promise2];promisesarray as an iterable of promises, but an array of a union type, which causes the mismatch in typing. Like what Christoph has suggested, you can simply castpromisesso that it has a type ofPromise<any>[], just as a hint to TypeScript.anyas Terry wrote in his comment.awaitedhas been reverted, so we'll have to wait a bit longer and use one of the workarounds.