当我尝试定义原型函数时,我得到: error TS2339: Property ‘applyParams’ does not exist on type ‘Function’. Function.prototype.applyParams = (params: any) = { this.apply(this, params);} 如何解决这个错误? 在功能
error TS2339: Property ‘applyParams’ does not exist on type
‘Function’.
Function.prototype.applyParams = (params: any) => {
this.apply(this, params);
}
如何解决这个错误?
在功能界面上定义它:interface Function {
applyParams(params: any): void;
}
并且您不想使用箭头函数,因此不会将其绑定到外部上下文.使用常规函数表达式:
Function.prototype.applyParams = function(params: any) {
this.apply(this, params);
};
现在这将工作:
const myFunction = function () { console.log(arguments); };
myFunction.applyParams([1, 2, 3]);
function myOtherFunction() {
console.log(arguments);
}
myOtherFunction.applyParams([1, 2, 3]);
