Typescript: Typescript error “class is not a constructor”

Created on 1 Jun 2016  Â·  6Comments  Â·  Source: microsoft/TypeScript

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.

Here is the question I posted in stackoverflow... link

Question

All 6 comments

This is a bug in 1.8.10 but fixed in master. To clarify:

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.

I understand the problem.. I am closing the issue.... Thanks vary much Arnavion.....

I have the same problem even after compile using these parameters. @bharadwaj509 did you solve your problem?

@cbfranca Note that I said it's a bug in 1.8.10, so if you're using that you will of course hit it.

What version of tsc was the fix released in?

@seanthebean i had same problem and i fixed it you can do somthing like that

export class Cars {
constructor(public len: number = null,public wid: number = null) {
this.len = len
this,wid = wid
}
}

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

Was this page helpful?
0 / 5 - 0 ratings