2017-12-18 15 views
0

最近サポートされていないNuGetパッケージを置き換えるためにNPMの型を使用するようにアップグレードしました。正常に動作するために使用次のコード:プロパティxがタイプに存在しません。機能

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

ない答えが、これはどのような[ 'bind'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind)ではありませんありますか? –

+0

javascriptの出力を見て、 'defer'関数を使用するコードが定義の後に出力されていることを確認してください。ファイルが間違った順序で出力されている場合は、定義の前に使用され、エラーが発生します。 –

答えて

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) 
    }; 
} 
関連する問題