是否可以为以下JSON创建一个Typescript接口,其中JSON对象的关键字发生了变化?
我很难定义一个变化的键(userFilter
,activityFilter
在我的例子中)作为一个字段名在typescript定义
{
"userFilter": {
"label": "Users",
"options": [{
"label": "Admin",
"value": "ADMIN"
},
{
"label": "Viewer",
"value": "VIEWER"
}
]
},
"activityFilter": {
"label": "Activity",
"options": [{
"label": "Active",
"value": "ACTIVE"
},
{
"label": "Inactive",
"value": "INACTIVE"
}
]
}
}
是的,你可以尝试这样做:
interface optionsType {label:string, value:string}
interface test {
[key:string]:{
label:string,
options:optionsType[]
}
}