【发布时间】:2019-09-16 06:48:33
【问题描述】:
我想扩展创建“isEmpty”的对象方法。
// typings/object.d.ts
declare global {
interface Object {
isEmpty(): boolean;
}
}
Object.prototype.isEmpty = function (this: Object) {
for (let key in this) {
if (this.hasOwnProperty(key)) {
return false;
}
}
return true;
};
然后我想在我的源代码中使用它:
let myEmptyDict = {};
let myFullDict = {"key": "value"};
console.log(myEmptyDict.isEmpty()); // true
console.log(myFullDict.isEmpty()); // false
看起来 isEmpty 没有定义,我该如何解决? 我正在使用 Typescript 3.6.2。
【问题讨论】:
-
我建议不要在生产中进行猴子补丁;即使可以在打字稿中“输入”
标签: typescript