[英]object of object interface typescript
I have an object for an array that can be the following我有一个 object 的数组,可以是以下
{
'value1': 1,
'value2': 2
}
or或者
{
'value1': 'a',
'value2': 'b'
}
or或者
{
'value1': {'sub1': 1, 'sub2': 2},
'value2': {'sub1': 1, 'sub2': 2}
}
I wanted to type this like this:我想这样输入:
export interface TableRow {
[key: string]: string | number | ([key: string] : string) | ([key: string] : number)
}
but it doesn't work.但它不起作用。
Is this the only way possible?这是唯一可能的方法吗?
export interface TableRow {
[key: string]: string | number | object
}
i think it better to do this neatly with two interfaces我认为最好用两个接口巧妙地做到这一点
export interface TableCell {
[key: string]: string | number
}
export interface TableRow {
[key: string]: string | number | TableCell
}
Replace ()
with {}
:将
()
替换为{}
:
export interface TableRow {
[key: string]: string | number | { [key: string]: string } | { [key: string]: number }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.