Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript error "class is not a constructor"

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
like image 554
Baradwaj Aryasomayajula Avatar asked May 31 '16 19:05

Baradwaj Aryasomayajula


People also ask

Is not a constructor error?

The JavaScript exception "is not a constructor" occurs when there was an attempt to use an object or a variable as a constructor, but that object or variable is not a constructor.

How do I create a constructor in TypeScript?

The TypeScript docs have a great example of constructor usage: class Greeter { greeting: string; constructor(message: string) { this. greeting = message; } greet() { return "Hello, " + this. greeting; } } let greeter = new Greeter("world");

How do I export a TypeScript class?

Use named exports to export multiple classes in TypeScript, e.g. export class A {} and export class B {} . The exported classes can be imported by using a named import as import {A, B} from './another-file' . You can have as many named exports as necessary in a file.


1 Answers

(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.

like image 101
Arnavion Avatar answered Sep 28 '22 00:09

Arnavion