Typescript error "class is not a constructor"

I run the following typescript code in the target ES6 environment and it says "Cars are not constructors"

I followed the link and tried to change the target environment to ES5. It is working fine. Can someone tell me why it is not working for the 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: "Cars are not constructors" in the getSize function.

By the way, I am trying to download all files using Systemjs.

By the way, I get an error message in the browser ........ I am not compiling it ...

Here is the compiled code 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 
+6
source share
3 answers

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

This is a bug in TS 1.8.10 and is 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 with var Cars , which is undefined .

In the main output for (1) , Cars = class Cars { instead, so it assigns var Cars and getSize() .

+2
source

I'm not sure, but I think it depends on the TypeScript version.

Try declaring it like this:

 class Cars { constructor(public len: number,public wid: number) { } } export function getSize(): Cars { return new Cars(20, 30); }; export { Cars }; 
+2
source

Make sure you do not have .js and .ts in the same directory. Sometimes this can be caused by your IDE.

0
source

All Articles