无法启动typescript编译文件

Ale*_*kov 5 javascript typescript

我已经使用PhantomJs将大型JS项目转换为打字稿(作为IC#程序员).问题是解释器(phantomjs)在执行此js文件时失败.

D:\My\phantomjs-1.9.7-windows\phantomjs.exe --load-images=false --ssl-protocol=any --web-security=no --cookies-file=cookies C:\Users\alex\Projects\robot\bo.js
TypeError: 'undefined' is not an object (evaluating 'b.prototype')
Run Code Online (Sandbox Code Playgroud)

代码是:

var __extends = this.__extends || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]
    function __() { this.constructor = d; }
    __.prototype = b.prototype; // <<< here
    d.prototype = new __();
};
Run Code Online (Sandbox Code Playgroud)

所以.我认为这个问题与继承有一定关系.有没有人遇到过这个问题?任何帮助表示赞赏.谢谢.

Fen*_*ton 12

导致此错误的最常见原因是您以错误的顺序加载文件...例如......

档案A.

class ExampleClass {
    someMethod() {
        alert('Hello World');
    }
}
Run Code Online (Sandbox Code Playgroud)

档案B.

class ExampleSubClass extends ExampleClass {

}
Run Code Online (Sandbox Code Playgroud)

如果您File B之前加载File A,您将得到您描述的确切错误.(这包括忘记加载File A或加载File A之后File B).

修复

如果要将所有文件合并到一个文件中(并且可能正在使用_references.ts文件),请确保引用的顺序正确.

/// <reference path="file-a.ts" />
/// <reference path="file-b.ts" />
Run Code Online (Sandbox Code Playgroud)

如果您使用的是脚本标签,那就是类似的修复(确保您使用.js扩展并检查加载顺序)......

<script src="file-a.js"></script>
<script src="file-b.js"></script>
Run Code Online (Sandbox Code Playgroud)