Typescript错误运行时错误:扩展时无法读取未定义的属性"prototype"

Mav*_*ick 34 javascript visual-studio asp.net-mvc-4 typescript

所以我在控制台中遇到了上述错误.它是由_super传递给__extends(在生成中.js)时未定义引起的.

这是一些可用于重现错误的测试代码:

//This is the entirety of the file Test.ts
module Test {
    export class Test1 {
        public Name: string;
        public Number: number;

        constructor() {

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在一个单独的文件中,我有一个继承自那个的类:

/// <reference path="Test.ts" />
module Test {
    export class Test2 extends Test1 {
        constructor() {
            super();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

<reference path...不应该需要(而不是),但我添加它,看它是否帮助了(事实并非如此).

文件包含在正确的顺序(Test.ts然后Test2.ts)通过BundleConfig(运行有或没有优化没有任何影响).

我可能是一个巨大的菜鸟,但我没有丝毫的线索我搞砸了什么.我在网上找到的这个问题的所有其他实例都来自使用命令行编译器将多个Typescript文件合并为一个文件的人.我正在使用捆绑器来做到这一点,但即使我没有将它们组合起来,我也会遇到完全相同的问题.

请帮助我,我的智慧结束了!

根据要求,这里是编译的javascript:Test.js:

//This is the entirety of the file Test.ts
var Test;
(function (Test) {
    var Test1 = (function () {
        function Test1() {
        }
        return Test1;
    })();
    Test.Test1 = Test1;
})(Test || (Test = {}));
//# sourceMappingURL=Test.js.map
Run Code Online (Sandbox Code Playgroud)

Test2.js:

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;
    d.prototype = new __();
};
/// <reference path="Test.ts" />
var Test;
(function (Test) {
    var Test2 = (function (_super) {
        __extends(Test2, _super);
        function Test2() {
            _super.call(this);
        }
        return Test2;
    })(Test.Test1);
    Test.Test2 = Test2;
})(Test || (Test = {}));
//# sourceMappingURL=Test2.js.map
Run Code Online (Sandbox Code Playgroud)

Rya*_*ugh 25

发生这种情况的可能原因:

  1. BundleConfig以正确顺序连接文件的四重检查.到目前为止,这是该错误的最常见原因.
  2. 确认您没有任何顶级export指令Test.ts.这将导致文件成为外部模块,并且Test1将不再可见.

如果做不到这一点,您应该将发出的JavaScript发布到问题中,以便我们可以诊断导致问题的原因.

  • 这使我指出了解决同一问题的正确方向. (2认同)

sup*_*jos 18

今天发生了这个错误.不确定什么是OP场景,但在我的团队的情况下,我们有:

  1. TypeScript v1.8.10
  2. 基于Webpack的开发构建与连接,源映射,没有优化/ uglification
  3. Angular 2依赖注入
  4. 在同一个文件中定义的基类和派生类(比方说dependencies.ts)
  5. 派生类定义的基类
  6. 没有编译错误也没有警告
  7. 控制台日志显示 Uncaught TypeError: Cannot read property 'prototype' of undefined
  8. 调用堆栈指向__extends另一个类的最后一行的内部函数,在另一个文件中(比如说client.ts),将它们作为依赖项导入

在代码中:

// dependencies.ts

import { Injectable } from 'angular2/core';

@Injectable()
export class LocalStorageService extends BaseStorageService {
  constructor() {
    super(localStorage);
  }
}

class BaseStorageService {
  constructor(private storage: Storage) {}
  // ...
}
Run Code Online (Sandbox Code Playgroud)

和:

// client.ts

import { Injectable } from 'angular2/core';
import { LocalStorageService } from './dependencies';

@Injectable()
export class AuthStorageService {

  constructor(private permanentStorage: LocalStorageService) { }
  // ...

} // <-- call stack pointed here with inner '__extends' function
Run Code Online (Sandbox Code Playgroud)

通过派生类之前定义基类来解决问题.快速搜索和阅读后,这似乎与已知(和未解决的?)TypeScript问题有关,例如#21#1842.

HTH