【问题标题】:prototype in typescript:Element implicitly has an 'any' type because type '_BaseOption' has no index signaturetypescript 中的原型:Element 隐式具有“any”类型,因为类型“_BaseOption”没有索引签名
【发布时间】:2017-12-09 20:26:21
【问题描述】:
我想在打字稿中使用原型。
export class base{
constructor() {
base.prototype["g"] = new option({});
}
}
第 3 行显示:[ts] 元素隐式具有 'any' 类型,因为类型 'base' 没有索引签名。
救命!
【问题讨论】:
标签:
javascript
typescript
【解决方案1】:
您可以将索引签名添加到您的base 类:
export class base{
[prop: string]: option
constructor() {
base.prototype["g"] = new option({});
}
}
但是这样做意味着您通过base 实例上的索引签名访问的任何属性也将输入为option。示例:
let doesNotExist = new base()["doesNotExist"]; // Will compile fine without throwing error.
如果您只想向原型添加一组有限的属性,您可以单独添加这些属性:
export class base{
g: option
constructor() {
base.prototype["g"] = new option({});
}
}
let g = new base()["g"] // OK
let doesNotExist = new base()["doesNotExist"] // Error