对象接口打字稿的对象

时间:2020-05-21 03:09:49

标签: typescript

我有一个数组对象,可以是以下对象

{
 'value1': 1,
 'value2': 2
}

{
 'value1': 'a',
 'value2': 'b'
}

{
 'value1': {'sub1': 1, 'sub2': 2},
 'value2': {'sub1': 1, 'sub2': 2}
}

我想这样输入:

export interface TableRow {
  [key: string]: string | number | ([key: string] : string) | ([key: string] : number)
}

但它不起作用。

这是唯一可行的方法吗?

export interface TableRow {
  [key: string]: string | number | object
}

2 个答案:

答案 0 :(得分:2)

我认为最好使用两个界面整齐地完成此操作

export interface TableCell {
    [key: string]: string | number
}

export interface TableRow {
    [key: string]: string | number | TableCell
}

答案 1 :(得分:1)

()替换为{}

export interface TableRow {
    [key: string]: string | number | { [key: string]: string } | { [key: string]: number }
}