With TypeScript 1.6 and native es6 Promises I'm getting an error whenever I use Promise.all([])
with two different return types. For example:
let onePromise:Promise<string[]> = getOne();
let twoPromise:Promise<MyObject> = getTwo();
Promise.all([onePromise, twoPromise])
.then((values:[string[], MyObject]) => {
let one:string[] = values[0];
let two:MyObject = values[1];
// do stuff
});
In that example I get an error on the Promise.all
line from the TypeScript compiler error TS2453: The type argument for type parameter 'R' cannot be inferred from the usage. Consider specifying the type arguments explicitly. Type argument candidate 'string[]' is not a valid type argument because it is not a supertype of candidate 'MyObject'. Property 'length' is missing in type 'MyObject'.
I actually have another example of this where the second and third sentences of the error are different, but that first sentence is the same. So basically I'm wondering what the syntax is for 'specifying the type arguments explicitly'. I can't seem to figure it out. The code runs fine, but I would like to get rid of this transpiler warning.
Here's a workaround:
let onePromise:Promise<string[]> = getOne();
let twoPromise:Promise<MyObject> = getTwo();
Promise.all<string[] | MyObject>([onePromise, twoPromise])
.then((values:[string[], MyObject]) => {
let one:string[] = values[0];
let two:MyObject = values[1];
// do stuff
});
The vertical bar is used to specify a value that can be one of several types.