函数原型当我试图定义一个函数的原型,我得到:定义与打字稿
错误TS2339:房产“applyParams”上不存在类型 “功能”。
Function.prototype.applyParams = (params: any) => {
this.apply(this, params);
}
如何解决这个问题?
函数原型当我试图定义一个函数的原型,我得到:定义与打字稿
错误TS2339:房产“applyParams”上不存在类型 “功能”。
Function.prototype.applyParams = (params: any) => {
this.apply(this, params);
}
如何解决这个问题?
定义它的函数接口:
interface Function {
applyParams(params: any): void;
}
而你不希望使用箭头功能,使this
不会被绑定到外部环境。使用正则函数表达式:
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]);
我也尝试使用一个接口,它仍然得到错误。我尝试过接口函数和接口FunctionConstructor – Alexandre
@Alexandre你使用的是外部模块吗?在定义文件(* .d.ts *文件)中定义接口并在您的应用程序中引用 –
@Alexandre您可以在[这里]看到这个工作(https://www.typescriptlang.org/play/#src=interface %20Function%20%7B%0D 0A%%20%20%20个%20applyParams(PARAMS%3A%20any)%3A%20void%3B%0D 0A%%7D%0D 0A%%0D%0AFunction.prototype.applyParams% 20%3D%20function%20(PARAMS%3A%20any)%20%7B%0D 0A%%20%20%20个%20this(... PARAMS)%3B%0D 0A%%7D%3B%0D 0A% %0D%0Aconst%20func%20%3D%20function%20()%20%7B%20console.log(参数)%3B%20%7D%3B%0D%0Afunction%20myOtherFunc()%20%7B%0D% 0A%20%20%20%20console.log(参数)%3B%0D 0A%%7D%0D 0A%%0D%0Afunc.applyParams(%5B1%2C%202%2C%203%5D)%3B%0D %0AmyOtherFunc.applyParams(%5B1%2C%202%2C%203%5D)%3B) –
试试这个:http://stackoverflow.com/a/28020863/1142380 我不认为你需要的“'prototype.'”部分 – ToastyMallows
@ ToastyMallows,但然后我得到错误TS2339:属性'applyParams'在类型'FunctionConstructor'上不存在。即使使用接口FunctionConstructor applyParams(params:any):any; } – Alexandre