简体   繁体   English

object 接口 typescript 的 object

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM