打字稿错误运行时错误:扩展时无法读取的未定义的属性“原型” [英] Typescript error runtime error: Cannot read property 'prototype' of undefined when extending

查看:196
本文介绍了打字稿错误运行时错误:扩展时无法读取的未定义的属性“原型”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我得到在控制台上面的错误。它是由 _super 引起的,当它传递给 __扩展(在生成的.js <正在未定义/ code>)。

下面是一些测试code,可用于重现错误:

  //这是文件Test.ts的全部
模块测试{
    出口类的Test1 {
        公共产品名称:串;
        公共号码:号码;        构造函数(){        }
    }
}

然后在一个单独的文件I具有从一个继承的类

  ///&lt;参考路径=Test.ts/&GT;
模块测试{
    出口类的Test2 Test1的扩展{
        构造函数(){
            超();
        }
    }
}

&lt;参考路径... 不应该需要(而不是),但我加了它,看它是否帮助了(事实并非如此)。

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

我可能是一个巨大的小白,但我没有丝毫的线索,我搞砸了。这个问题我在网上找到的所有其他实例从使用命令行编​​译器将多个打字稿文件合并成一个单一的文件乡亲。我使用的是打捆要做到这一点,但即使我不将它们组合起来,我得到确切同样的问题。

请帮帮我,我在我无计可施!

按照要求,这里的JavaScript的编译:
Test.js:

  //这是文件Test.ts的全部
VAR测试;
(功能(测试){
    VAR的Test1 =(函数(){
        Test1的功能(){
        }
        Test1的回报;
    })();
    Test.Test1 = Test1的;
})(试验||(试验= {}));
//#sourceMappingURL = Test.js.map

Test2.js:

  VAR __extends =这个.__延伸||功能(D,B){
    为(b中变种p)的,如果(b.hasOwnProperty(p)的情况)d [P] = B [P];
    函数__(){this.constructor = D; }
    __原型= b.prototype;
    d.prototype =新__();
};
///&lt;参考路径=Test.ts/&GT;
VAR测试;
(功能(测试){
    VAR的Test2 =(函数(_super){
        __extends(的Test2,_super);
        功能的Test2(){
            _super.call(本);
        }
        回到Test2的;
    })(Test.Test1);
    Test.Test2 = Test2的;
})(试验||(试验= {}));
//#sourceMappingURL = Test2.js.map


解决方案

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


  1. 四,检查 BundleConfig 被连接起来以正确的顺序的文件。这是迄今为止该错误的最常见的原因。

  2. 确认你没有任何顶级的 Test.ts 导出指令。这会导致文件变成外部模块和测试1 将不再可见。

如果做不到这一点,你应该张贴发射的JavaScript的问题,所以我们可以诊断是什么引起的问题。

So I'm getting the above error in the console. It's caused by _super being undefined when it's passed to __extends (in the generated .js).

Here's some test code that can be used to reproduce the error:

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

        constructor() {

        }
    }
}

Then in a separate file I have a class that inherits from that one:

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

The <reference path... shouldn't be needed (and isn't), but I added it to see if it helped (it didn't).

The files are included in the correct order (Test.ts then Test2.ts) via BundleConfig (running with or without optimisations doesn't have any effect).

I am probably being a giant noob, but I haven't the slightest clue what I've messed up. All the other instances of this problem I've found online are from folks using the command line compiler to combine multiple Typescript files into one single file. I'm using the bundler to do that, but even when I don't combine them, I get the exact same issue.

Please help me, I'm at my wits end!

As requested, here's the compiled 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

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

解决方案

Possible reasons this is happening:

  1. Quadruple-check that BundleConfig is concatenating the files in the correct order. This is by far the most common cause of that error.
  2. Verify you don't have any top-level export directives in Test.ts. This would cause the file to become an external module and Test1 would no longer be visible.

Failing that, you should post the emitted JavaScript to the question so we can diagnose what's causing the issue.

这篇关于打字稿错误运行时错误:扩展时无法读取的未定义的属性“原型”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆