我是打字稿的新手,我有一个 JSON 对象,如下所示。我想将下面的对象映射到下面的通用接口,但我确定这是不正确的。帮助我构建一个可以处理以下映射的通用接口/类。
"parentValue": {
"childValue1": {
"resource": "childValue1",
"application": "childValue1",
"permissions": [
"READ"
],
"attributeConstraints": {},
"attributeConstraintsCount": 0
},"childValue2": {
"resource": "childValue2",
"application": "childValue2",
"permissions": [
"READ"
],
"attributeConstraints": {},
"attributeConstraintsCount": 0
},
"childValue3": {
"resource": "childValue3",
"application": "childValue3",
"permissions": [
"READ"
],
"attributeConstraints": {},
"attributeConstraintsCount": 0
}
}
interface ParentValue{
childValue: ChildValue<T>
}
export interface ChildValue<T>{
childDetails: ChildDetails
}
export ChildDetails{
resource: string;
application: string;
permissions: string[];
attributeConstraints: AttributeConstraints;
attributeConstraintsCount: number;
}
你的情况没有什么通用的。简单地写这样的东西:
export ChildDetails {
resource: string;
application: string;
permissions: string[];
attributeConstraints: AttributeConstraints;
attributeConstraintsCount: number;
}
interface ParentValue {
childValue1: ChildDetails,
childValue2: ChildDetails,
childValue3: ChildDetails,
}
如果您有无限数量的,childValueX
那么没有什么比:
interface ParentValue {
[key: string]: ChildDetails
}
但是你不能告诉编译器key
看起来像childValue\d+
.
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句