假设我有一个函数返回一个列表,其中包含可变数量的列表,每个列表包含首先可变数量的列表,然后将单个对象作为名单的最后一名成员。在类型别名中声明未知数量的列表
这里有几个输出的例子:
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
,其中的每一个具有类型number
。
我该如何创建一个准确,专门显示此类型的别名?
眼下,这就是我创造:
type Output = [
[
any[], // this is what I need help with
{ x: number, y: number }
][]
]
我认为你现在可以做的最好的是(任何[] | {x:number; y:number;})[]。但也许在未来休息/传播属性可以帮助https://github.com/Microsoft/TypeScript/issues/2103 – Magu
是的,这将工作,直到有更好的事情出现。 – gliemezis