假设我有一个返回列表的函数,其中包含可变数量的列表,每个列表首先包含可变数量的列表,然后是作为列表最后一个成员的单个对象。
这里有几个输出示例:
output1 = [
[
[1, 2, 3],
{ x: 1, y: 2 }
],
[
[4, 5, 6],
{ x: 3, y: 4 }
],
[
[7, 8, 9],
{ x: 5, y: 6 }
]
];
output2 = [
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
// however many more lists there are
{ x: 1, y: 2 }
],
[
['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h', 'i'],
// however many more lists there are
{ x: 3, y: 4 }
],
// however many more lists there are
];
列表的内容可以是 any
类型,但是每个对象都有属性 x
和 y
,每个属性的类型都是 编号
。
我如何创建一个类型别名来准确、具体地显示这一点?
现在,这是我创建的:
type Output = [
[
any[], // this is what I need help with
{ x: number, y: number }
][]
]
最佳答案
你的结构设计的不好,为什么顶层结构是数组而不是对象?
您是否坚持该结构(即:它是第 3 方库的结果或 json 结果),还是您可以更改它?
如果您可以更改它,那么我建议您这样做,因为您将能够拥有更多的控制权。
type Output = Array<Array<number[] | { x: number, y: number }>>;
let output1: Output = [
[
[1, 2, 3],
{ x: 1, y: 2 }
],
[
[4, 5, 6],
{ x: 3, y: 4 }
],
[
[7, 8, 9],
{ x: 5, y: 6 }
]
];
关于typescript - 在类型别名中声明未知数量的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38237514/