Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
C:\Users\Alex\WebstormProjects\Tutorial01\Tests>tsc TS2.0.6Bug.ts --target ES6 TS2.0.6Bug.ts(28,21): error TS2322: Type 'void | IFoo[]' is not assignable to ty pe 'IFoo[]'. Type 'void' is not assignable to type 'IFoo[]'.
interface IFoo{
square:number
}
(
async () => {
async function squareFun(i:number) : Promise<IFoo>{
if (i === 2)
throw 'squareFun1 throw 2';
return Promise.resolve({square: i * i});
}
async function getSquares3(): Promise<IFoo[]>{
//I like this as I don't need to use Promise.all<IFoo>
var squares = await Promise.all([squareFun(1), squareFun(4)]);
return squares;
}
async function getSquares(): Promise<IFoo[]>{
try {
var promises = [1, 2, 3].map(n => squareFun(n));
var squares = Promise.all(promises);
//It can be compiled
//var cs : Promise<IFoo[]> = squares.catch(err => console.log('Catched error Promise.catch:' + err));
//This cannot be compiled
var cs = squares.catch(err => console.log('Catched error Promise.catch:' + err));
var t : IFoo[] = await cs;
return t;
}
catch(err){
console.log('Catched error try/catch:' + err);
}
}
var squares = await getSquares();
if (squares)
squares.forEach(s => console.log('square:' + s.square));
}
)();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment