2

Resig の 'extend' を使用する既存のコードを参照しようとしていますが、大量のエラーが発生します

------ test.ts --------

/// <reference path="myclass.js" />
var m = new MyClass (3);

------ myclass.js --------

/// <reference path="class.js" />

var MyClass = Class.extend({

    init: function (i)
    {
        this.i = i;
    },
})

------ class.js --------

(copied from http://ejohn.org/blog/simple-javascript-inheritance/)

エラー:

Supplied parameters do not match any signature of call target
The name 'Class' does not exist in the current scope
The property 'extend' does not exist on value of type '() => void'
The name 'Class' does not exist in the current scope

最終的には拡張ベースのコードを TypeScript に書き直したいと思いますが、それまでは新しいコードからどのように参照すればよいでしょうか?

これは、より深い疑問を投げかけていると思います-既存の JavaScript コードの型エラーについて不平を言うのはなぜですか?

4

1 に答える 1

3

TypeScript は通常、外部の JavaScript コードから型を推測できません。

TypeScript が型の形状を認識できるように、呼び出す「extend」コードの形状を宣言する必要があります。

declare class Class {
    static extend(body: any);
}

それを直接ソース ファイルに入れるか (単一ファイル プロジェクトの場合)、より適切には、ソース ファイルから参照する '.d.ts' ファイルに入れることができます。

于 2012-10-02T17:23:55.267 に答える