如何将多种数据类型分配给打字稿 + angular 11 中的一个变量?

时间:2021-06-18 07:17:24

标签: angular typescript

我有一个场景,在 promiseAll 下,我发送两个请求,并在 then 块中组合响应返回,因此我需要定义两个数据类型。我是这样定义的

Promise.all<FirstType[], SecondType>([this.firstAPIHit, this.secondAPIHit(Id)]).then((data: [FirstType[], SecondType]): void => {

FirstTypeArray List 例如:[{Id: 1, Name: "Test"}, {Id: 2, Name: "Random"}] >

SecondTypeobject 例如:{Id: "12345", city: null, location: Array(1)}

作为一项功能,它在 Angular 8 之前运行良好,但我最近将其迁移到 Angular 11 并开始抛出错误

 0:0  error  Parsing error: Cannot read property 'map' of undefined

调试后我发现如果我删除此类型声明,则会立即删除错误。但我不认为删除它。

在相同的上下文中我可以将两者分配给同一个变量有什么帮助吗?

响应包含 data---> (2) [Array(3), {…}]


新编辑:

我在其他地方也发现了同样的错误,我发现我像下面这样定义的地方现在显示错误:

abc: [number, string][]

private myFunction(chart): [IChartData[], number, number] {...}

希望它可以帮助某人回答我的问题!

0 个答案:

没有答案