Typescript错误“类不是构造函数” [英] Typescript error "class is not a constructor"

查看:426
本文介绍了Typescript错误“类不是构造函数”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ES6目标环境中运行以下类型的脚本代码,它表示汽车不是构造函数



我已经遵循链接,并尝试将目标环境改为ES5。工作正常有人可以告诉为什么它不适用于目标ES6。



这是我的TypeScript代码:

  export class Cars {
constructor(public len:number,public wid:number){}
}

export function getSize():Cars {
返回新车(20,30);
};

错误是函数getSize中的汽车不是构造函数。



顺便说一句,我试图加载所有的文件与Systemjs。



顺便说一下,我在浏览器中收到错误......不是在编译它的时候...



这是上面的编辑代码....

  System.register([],function(exports_1,context_1){
use strict;
var __moduleName = context_1& context_1.id;
var Cars;
function getSize(){
return new Cars(20,30);
}
exports_1(getSize,getSize) ;
return {
setters:[],
execute:function(){
class Cars {
constructor(len,wid){
this。 len = len;
this.wid = wid;
}
}
;
exports_1(汽车,汽车);
}
}
});
//#sourceMappingURL = Cars.js.map


解决方案

(从您打开的GH问题复制我的信息)



这是TS 1.8.10中的一个错误,修正为主。



tsc -t es6 ./foo.ts -m系统



in 1.8.10给出:

  System.register([],function(exports_1,context_1){
use strict;
var __moduleName = context_1& & context_1.id;
var Cars;
function getSize(){
return new Cars(20,30);
}
exports_1(getSize getSize);
return {
setters:[],
execute:function(){
class Cars {//(1)
constructor(len,wid) {
this.len = len;
this.wid = wid;
}
}
exports_1(汽车,汽车);
}
}
});

所以 getSize 最终使用 var Cars 这是 undefined



在master中输出(1)相反 Cars = class Cars {所以分配给 var Cars getSize()的作品。


I am running the following typescript code in the ES6 target environment and it says that "Cars is not a constructor"

I have followed the link and tried changing the target environment to ES5. It is working fine. Can some one tell why it is not working for target ES6.

Here is my TypeScript code:

export class Cars {
    constructor(public len: number,public wid: number) { }
}

export function getSize(): Cars {
    return new Cars(20, 30);
};

Error is "Cars is not a constructor" in the function getSize.

By the way I am trying to load all the files with Systemjs.

By the way I am getting the error in the browser........ Not while compiling it...

Here is the compiled code of the above typescript....

System.register([], function(exports_1, context_1) {
    "use strict";
    var __moduleName = context_1 && context_1.id;
    var Cars;
    function getSize() {
        return new Cars(20, 30);
    }
    exports_1("getSize", getSize);
    return {
        setters:[],
        execute: function() {
            class Cars {
                constructor(len, wid) {
                    this.len = len;
                    this.wid = wid;
                }
            }
            ;
            exports_1("Cars", Cars);
        }
    }
});
//# sourceMappingURL=Cars.js.map

解决方案

(Copying my post from the GH issue you opened.)

This is a bug in TS 1.8.10 and fixed in master.

tsc -t es6 ./foo.ts -m system

in 1.8.10 gives:

System.register([], function(exports_1, context_1) {
    "use strict";
    var __moduleName = context_1 && context_1.id;
    var Cars;
    function getSize() {
        return new Cars(20, 30);
    }
    exports_1("getSize", getSize);
    return {
        setters:[],
        execute: function() {
            class Cars { // (1)
                constructor(len, wid) {
                    this.len = len;
                    this.wid = wid;
                }
            }
            exports_1("Cars", Cars);
        }
    }
});

So getSize ends up using the var Cars which is undefined.

In master the output for (1) is instead Cars = class Cars { so it assigns to the var Cars and getSize() works.

这篇关于Typescript错误“类不是构造函数”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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