我想为这个对象编写适当的接口。step的关键应该是动态的,其值是布尔值。这就是目标:
const obj = {
A: {
step: {
a: true,
b: true,
c: true,
},
},
B: {
step: {
a: true,
d: true,
},
},
C: {
step: {
e: true,
f: true,
g: true,
},
},
}
到目前为止,我已经尝试过了,但是我仍然有一个错误。
interface OBJ_INTERFACE {
side: {
step: {
[key: string]: boolean,
}
}
}
发布于 2020-04-22 03:54:28
发布于 2020-04-22 03:50:33
您没有详细说明错误,所以我只能假设它源于尝试使用接口:
const obj: OBJ_INTERFACE = {
A: {
step: {
...
}
}
}
在这种情况下,您的接口不考虑将side
属性动态命名为'A' | 'B' | 'C'
。
还可以尝试使side
动态:
interface OBJ_INTERFACE {
[key: string]: {
step: {
[key: string]: boolean
}
}
}
https://stackoverflow.com/questions/61363948
复制