【发布时间】:2016-12-15 11:38:40
【问题描述】:
在使用 for 循环时,我的 let 对象具有字符串类型,即使我正在迭代的对象是接口中定义的类型。
下面是我正在使用的代码。当尝试访问在接口上定义为字符串的 mapping.attribute 时,我收到错误 [Property 'attribute' does not exist on type 'string'.]
我有如下界面和功能:
interface IMapping {
attribute: string;
property: string;
}
mapAttributes(mappings: IMapping[], values) {
for (let mapping in mappings) {
if (mapping.hasOwnProperty("attribute")) {
console.log(this.attributes.find(attribute => attribute.name === mapping.attribute).value);
}
}
}
应该如何定义 for 循环,以便我可以使用已在我的接口中定义的属性?
【问题讨论】: