【发布时间】:2017-08-08 10:35:04
【问题描述】:
我想描述一些嵌套对象的接口。在不为嵌套对象创建接口的情况下如何做到这一点?
interface ISome {
strProp:string;
complexProp:{
someStrKeyWhichIsDynamic:{
value:string;
optValue?:string;
}
};
}
我也试过了(UPD:其实没关系)
interface ISome {
strProp:string;
complexProp:{
[someStrKeyWhichIsDynamic:string]:{
value:string;
optValue?:string;
}
};
}
但我不能分配像这样的对象
let dynamicStrKey = 'myKey';
{
strProp:'str',
complexProp:{
[dynamicStrKey]:{
value:'something here',
optValue: 'ok, that too',
}
};
到带有ISome 类型的变量,没有类型断言<ISome>。至少 WebStorm 将此分配突出显示为错误。
如何正确描述嵌套对象?
【问题讨论】:
标签: typescript types type-conversion webstorm typescript2.0