[英]Adding custom method to Object.prototype in TypeScript
我有以下代码:
Object.prototype.custom = function() {
return this
}
它在 JavaScript 中工作得很好,但是当我把它放在 TypeScript 中时,我得到了这个错误:
Property 'custom' does not exist on type 'Object'.ts(2339)
如何绕过或解决此投诉?
为了实验(不建议在生产中,IMO),您可以ignore
它或扩展Object
(又名扩充)
// @ts-ignore
Object.prototype.custom = function() {
return this
}
interface Object {
custom2(): Object;
}
Object.prototype.custom2 = function() {
return this
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.