Created
October 30, 2016 22:51
-
-
Save Alex2357/b84cd3018bf4e490dc57f01cf50b55aa to your computer and use it in GitHub Desktop.
No bug
C:\Users\Alex\WebstormProjects\Tutorial01\Tests>tsc TS2.0.6NoBug.ts --target ES6 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[]'. C:\Users\Alex\WebstormProjects\Tutoria…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<IFoo[]> = Promise.all(promises);//type Promise<IFoo[]> not necessary, just added to verify that it is Promise<IFoo[]> | |
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