繁体   English   中英

在 TypeScript 中向 Object.prototype 添加自定义方法

[英]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
}

TS游乐场

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM