我在ES6目标环境中运行以下打字稿代码,它说“汽车不是构造函数”
我已经按照link并尝试将目标环境更改为ES5.它工作正常.有人可以说明为什么它不适用于目标ES6.
这是我的TypeScript代码:
export class Cars {
constructor(public len: number,public wid: number) { }
}
export function getSize(): Cars {
return new 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("Cars",Cars);
}
}
});
//# sourceMappingURL=Cars.js.map
解决方法
(从
the GH issue you opened.复制我的帖子)
这是TS 1.8.10中的一个错误,并在master中修复.
tsc -t es6 ./foo.ts -m system
在1.8.10给出:
System.register([],execute: function() {
class Cars { // (1)
constructor(len,wid) {
this.len = len;
this.wid = wid;
}
}
exports_1("Cars",Cars);
}
}
});
所以getSize最终使用未定义的var Cars.
在master中,(1)的输出是Cars = class Cars {因此它分配给var Cars和getSize()工作.