如何将泛型对象映射到typescript类
我是typescript新手,我有一个JSON对象,如下所示。我想将下面的对象映射到泛型 接口如下,但我确信这是错误的。请帮助我构造一个可以处理以下映射的通用接口/类 JSON对象 类型脚本接口如何将泛型对象映射到typescript类,typescript,Typescript,我是typescript新手,我有一个JSON对象,如下所示。我想将下面的对象映射到泛型 接口如下,但我确信这是错误的。请帮助我构造一个可以处理以下映射的通用接口/类 JSON对象 类型脚本接口 接口父值{ childValue:childValue } 导出接口子值{ childDetails:childDetails } 导出儿童详细信息{ 资源:字符串; 应用:字符串; 权限:字符串[]; attributeConstraints:attributeConstraints; Attribu
接口父值{
childValue:childValue
}
导出接口子值{
childDetails:childDetails
}
导出儿童详细信息{
资源:字符串;
应用:字符串;
权限:字符串[];
attributeConstraints:attributeConstraints;
AttributeConstraintCount:编号;
}
在您的案例中没有通用的东西。简单地写下这样的话:
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+
export ChildDetails {
resource: string;
application: string;
permissions: string[];
attributeConstraints: AttributeConstraints;
attributeConstraintsCount: number;
}
interface ParentValue {
childValue1: ChildDetails,
childValue2: ChildDetails,
childValue3: ChildDetails,
}
interface ParentValue {
[key: string]: ChildDetails
}