简体   繁体   中英

How does typescript type Promise.all()

How does typescript type Promise.all() In the resolved array of values, how is typescript able to infer the type of each item? I only see one Generic T in the type signature of Promise.all(), but why am I able to pass in promises that resolve to different types of values?

To elaborate, why does my promiseAll implementation throws typescript type error but the built-in one work like magic?

const promiseAll = function<T>(promises: Promise<T>[]): Promise<T[]> {
  const results: T[] = [];
  let completedCount = 0;
  return new Promise(function (resolve, reject) {
    promises.forEach(function(promise, index) {
      promise.then(function (value) {
        results[index] = value;
        completedCount += 1;
        if(completedCount === promises.length) {
          resolve(results);
        }
      }).catch(function (error) {
        reject(error);
      });
    });
  });
}

const promise1 = new Promise<number>(resolve => setTimeout(() => { resolve(1) }, 500));
const promise2 = new Promise<string>(resolve => setTimeout(() => { resolve("a") }, 500));



async function main() {
    const [value1, value2] = await Promise.all([promise1, promise2]);
    const [myValue1, myValue2] = await promiseAll([promise1, promise2]);
}

main();

TypeScript types Promise.all with 11 overloads, but this approach only allows up to a finite number of PromiseLike objects with different types to be passed in. One of the overloads is able to handle an array of any length filled with consistently typed PromiseLike objects, though. Here are two of the overloads for an example:

Promise.all<T1, T2, T3, T4>(values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>]): Promise<[T1, T2, T3, T4]>
Promise.all<T>(values: readonly (T | PromiseLike<T>)[]): Promise<T[]>

The Promise.all overloads with independently typed arguments seem to support only up to 9 distinctly typed elements in the values argument.

The code you've given as an example matches the second overload above quite closely, but has the same limitation in that it will only work if the type of every Promise in the array passed to Promise.all is the same.

Your example would compile if you told TypeScript you were passing an array of type Promise<number | string>[] Promise<number | string>[] to PromiseAll , because then all of your Promise s would be treated as having the same type. Though then of course the array you get back out would be of type (number | string)[] and you wouldn't know which element had which type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM