Reputation: 18551
Looking at the type definition of Promise.all
, I see 10 definitions:
/**
* Creates a Promise that is resolved with an array of results when all of the provided Promises
* resolve, or rejected when any Promise is rejected.
* @param values An array of Promises.
* @returns A new Promise.
*/
all<T1, T2, T3>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>]): Promise<[T1, T2, T3]>;
I only included the one with an array of length 3, but there also exist all<T1>
, all<T1, T2>
, all the way up to all<T1, T2, ..., T9, T10>
.
However, this doesn't match with the implementation of Promise.all
, which can take as an input an array longer than 10:
let myPromise = num => Promise.resolve(num);
let myPromisesArray = (new Array(20))
.fill()
.map((_,i) => myPromise(i));
Promise.all(myPromisesArray).then(console.log)
I'm not the worst developer in the world but still, I make the assumption that the Microsoft developers who produced the type definition of ES2015 know more about JS/TS than me, which begs the question:
Why the type definition of Promise.all doesn't match with neither its documentation nor its implementation?
Upvotes: 0
Views: 307
Reputation: 51957
The generic declaration up to 10 types is only accounting for non-uniform "tuples", or arrays where different indices have explicit independent types.
There is also a declaration that supports a uniformly typed array of unbounded length:
all<T>(values: (T | PromiseLike<T>)[]): Promise<T[]>;
The declarations using a tuple of up to length 10 are supposed to cover a reasonable amount of use-cases, and are due to a limitation of TypeScript as a language, since generic type inference for a tuple is a uniform array of an encompassing union type, which would use the signature above. These sort of generic declarations up to an arbitrary bound of non-homogeneous types are common-place in other langauges like C# as well. For example, Action<>
and Func<>
use a limit of 16 parameter types.
Upvotes: 4