typescript 打字稿错误“类不是构造函数”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37554119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Typescript error "class is not a constructor"
提问by Baradwaj Aryasomayajula
I am running the following typescript code in the ES6 target environment and it says that "Cars is not a constructor"
我在 ES6 目标环境中运行以下打字稿代码,它说“汽车不是构造函数”
I have followed the linkand tried changing the target environment to ES5. It is working fine. Can some one tell why it is not working for target ES6.
我已经按照链接并尝试将目标环境更改为 ES5。它工作正常。有人能说出为什么它不适用于目标 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.
错误是函数 getSize 中的“Cars 不是构造函数”。
By the way I am trying to load all the files with Systemjs.
顺便说一下,我正在尝试使用 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
采纳答案by Arnavion
(Copying my post from the GH issue you opened.)
(从您打开的 GH 问题复制我的帖子。)
This is a bug in TS 1.8.10 and fixed in master.
这是 TS 1.8.10 中的一个错误并在 master 中修复。
tsc -t es6 ./foo.ts -m system
tsc -t es6 ./foo.ts -m system
in 1.8.10 gives:
在 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("Cars", Cars);
}
}
});
So getSize
ends up using the var Cars
which is undefined
.
所以getSize
最终使用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.
在 master 中,输出(1)
是相反的,Cars = class Cars {
因此它分配给var Cars
和getSize()
工作。
回答by Jamel Toms
Make sure you don't have .js
and .ts
files in the same directory. This can be caused by your IDE sometimes.
确保您没有.js
和.ts
文件在同一目录中。这有时可能是由您的 IDE 引起的。
回答by Filip Koblański
I'm not sure but I think that this depends on the TypeScript version.
我不确定,但我认为这取决于 TypeScript 版本。
Please try declaring this like so:
请尝试这样声明:
class Cars {
constructor(public len: number,public wid: number) { }
}
export function getSize(): Cars {
return new Cars(20, 30);
};
export { Cars };