TypeScript在不存在的属性上抱怨,但它确实存在于接口上

时间:2016-11-11 14:52:02

标签: typescript

有这个奇怪的问题肯定似乎是TS中的一个错误。 得到

的错误

Error:(221, 76) TS2339:Property 'item' does not exist on type 'ISimpleListItem | { [key: string]: ISimpleListItem; }'. 但正如您从我的图片中看到的那样,我的界面在其定义中显然有item

所以错误来自于我 var itemSelected: AdnetTargetModel = this.simpleList.getSelected().item;

和界面如下图所示:

enter image description here

这是返回类型的方法:

public getSelected(): ISimpleListItem | { [key: string]: ISimpleListItem } { if (this.multiSelect) return this.m_metadata; for (let v in this.m_metadata) { if (this.m_metadata[v].selected == true) return this.m_metadata[v]; } }

解决问题的唯一方法是强制TS通过

识别正确的类型

var itemSelected: AdnetTargetModel = (this.simpleList.getSelected() as ISimpleListItem).item;

strang:/

问候

肖恩

0 个答案:

没有答案
相关问题