类型Function上不存在属性x

时间:2017-12-18 16:18:54

标签: typescript

我最近升级到使用NPM类型来替换不支持的NuGet包。以下代码用于正常工作:

interface Function
{
    /** Creates a function that calls the specified function using the specified "this" pointer. Useful in scenarios where we need to pass a function as  a callback, but specifying the value of "this".*/
    defer(thisArg: Object): Function;
}

Function.prototype.defer = function (thisArg: Object)
{
    var self = this;

    //return a function that calls the current function with the specific this argument
    return function()
    {
      self.call(thisArg)
   };

}

但现在我收到了错误Property 'defer' does not exist on type 'Function'.

该属性存在,因为我在接口中指定它,但编译器仍在抱怨。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

global object的声明合并规则略有变化(请注意页面末尾的示例)。它应该这样工作。

declare global {
    interface Function
    {
        /** Creates a function that calls the specified function using the specified "this" pointer. Useful in scenarios where we need to pass a function as  a callback, but specifying the value of "this".*/
        defer(thisArg: Object): Function;
    }
}

Function.prototype.defer = function (thisArg: Object)
{
    var self = this;

    //return a function that calls the current function with the specific this argument
    return function()
    {
      self.call(thisArg)
   };
}