为什么我的 TypeScript 有时会给我一个实例,而有时会给我一个 class?

Why does my TypeScript sometimes give me a instance and other times gives me a class?

我正在编写一个应用程序来学习 TypeScript。它在 Chrome 中运行良好,但在 Edge 中 运行ning 时出现问题。我有这个方法:

 set position(pos: Point) {
        const diffAsPoint = pos.minus(this.canvasPos);

        let diff: Vector2D = diffAsPoint.toVector2D(); // <--- this line
        if (diff instanceof Vector2D) {
            console.info("is a vector!");
        } else {
            console.info("not a vector!");
        }

我看到,有时 diff 是 Vector2D,而不是 的实例 Vector2D。显然,发生这种情况时,对其进行的任何操作都会导致 Object doesn't support property or method ...

方法toVector2D就是:

toVector2D(): Vector2D {
    return new Vector2D(this.x, this.y);
}

我不知道这是否有影响,但这里有一些背景:

更新------

使用 Edge 和调试工具并设置 'Always refresh from server',从网站加载时 (http://pacmanweb.azurewebsites.net/),我可以看到游戏 尝试 到 运行 在加载所有模块之前 。而在 Chrome 中,它似乎在 运行 开始游戏之前等待所有内容加载。

有什么想法吗?

该应用对我来说失败了,因为看起来您正在使用转译后的代码中保留的默认参数值:

  reset(isDemoMode = false) {

Edge 似乎不支持此 ES6 功能,而 Chrome 很可能支持。

tsc 应该会自动将其转换为 ES5。你应该检查你的配置,确保你没有将它设置为输出 es6 代码。

例如:

function obj(x = 5) {  
}

应转译为:

function obj(x) {
    if (x === void 0) { x = 5; }
}

还有一个link到playground