问题是,当它实际上可以自动解析类型时,它会强制我在Promise.all中指定类型。 getSquares3完美运行。 getSquares在没有指定泛型参数的情况下无法工作。 我使用的是WebStorm和2.0.6 TypeScript。该代码实际上可以编译,但WebStorm投诉最后一行返回语句。 代码说千言万语。
interface IFoo{
square:number
}
(
async () => {
async function square(i:number) : Promise<IFoo>{
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([square(1), square(4)]);
return squares;
}
async function getSquares(): Promise<IFoo[]>{
var promises = [1,2,3].map(n => square(n));
//I don't like this as I have to use Promise.all<IFoo>
var squares = await Promise.all<IFoo>(promises);
return squares;
}
squares = await getSquares3();
squares.forEach(s => console.log('square:' + s.square));
var squares = await getSquares();
squares.forEach(s => console.log('square:' + s.square));
}
)();