0

common.js ファイルに次のメソッドがあります。「common.js」ファイルを typescript ファイルにインクルードすると、最初の行でコンパイル エラーが発生します。

これをどのように解決すればよいですか?

Function.prototype.method = function (name, func) {
    ///<summary>
    ///Create a new method if it not ready exists
    ///</summary>
    if (!this.prototype[name]) {
        this.prototype[name] = func;
        return this;
    }
};

String.method('trim', function () {
    return this.replace(/^\s|\s+$/g, '');
});
4

1 に答える 1

2

Found it.

Just move the common.js file to a common.ts file. Then add the following code before the line which extends the 'prototype'

interface Function {
   method(name: string, func: Function): any;
}
于 2012-12-13T21:35:17.243 に答える