【发布时间】:2018-10-06 13:16:50
【问题描述】:
这行不通
interface String {
contains(s:string):boolean;
}
String.prototype.contains=(s:string):boolean=>this.indexOf(s)!==-1;
因为Property 'contains' does not exist on type 'String'
这有点令人惊讶,因为添加它是接口声明的全部内容。 http://www.typescriptlang.org/docs/handbook/declaration-merging.html 表示上述代码是合法的。据我通过检查lib.es2015.wellknown.d.ts 得知,String 位于全局命名空间中。
解决这个问题的正确方法是什么?看完Aluan Haddad的Extending third party module that is globally exposed我改写成这样
declare global {
interface String {
contains(s: string): boolean;
}
}
String.prototype.contains=(s:string):boolean=>this.indexOf(s)!==-1;
现在界面更改是正确的。但是现在'this' implicitly has type 'any' because it does not have a type annotation.
进一步的 cmets this 可以使用函数语法显式键入。
String.prototype.contains = function (this: string, s:string):boolean {
return this.indexOf(s)!==-1;
};
还需要注意的是,在调查过程中我发现contains是用名称includes实现的,并在lib.es2015.core.d.ts中声明
【问题讨论】:
-
可能不是,也许奥瑞莉亚在干扰。这可能是问题所在。
-
我在这个答案stackoverflow.com/a/43674912/1915893中详细介绍了这个主题
-
这不是 Aurelia,而是模块的使用。如果您在模块范围内,则需要包装接口声明以便将其合并到全局数组声明中。否则,您将定义一个名为 Array 的模块范围接口。有关完整的详细信息和示例,请参阅回答链接
-
我看到了您的编辑,但我不明白这与上课与否有什么关系。随便写吧,和类没有任何关系。
-
根据理解进行第二次阅读,我看到您确实顺便解释了它,但我认为这个答案要清楚得多。更重要的是,您已经解释了为什么它很重要。