我正在一个项目中使用TypeScript,有一个情况需要使用Promise.all(...)
,它会返回许多项的数组:
Promise.all(
firstRequest,
secondRequest,
...,
nthRequest
)
.then((array : [FirstType, SecondType, ..., NthType]) => {
// do things with response
});
现在,由于类型
[FirstType, SecondType, ..., NthType]
太长了,不能在此处定义,因此我想在其他地方定义它,并在该点使用它。
所以我尝试了:
export interface ResponseParams {
[0]: FirstType;
[1]: SecondType;
...
[n]: NthType;
}
同时:
.then((array : ResponseParams) => {
// do things with response
});
但我收到了这个错误:
我该如何将类型外部化并使我的代码更清晰呢?类型'ResponseParams'不是数组类型。
谢谢。
Promise
的解析类型未定义且您想保持代码更清晰时,它仍然可以很有用(至少在我看来)。 - Christian Vincenzo Traina