Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript class.default is not a constructor

Creating an Angular2 app, I am facing the following problem, when calling the constructor of another class inside the constructor of first class.

First Class code

import SecondClass from './second-class'

export class FirstClass {
    someVar:string;
    secondClass:SecondClass;

    constructor(firstClass?: FirstClass) {
        someVar='test';
        secondClass= new SecondClass;
    }
}

Second Class code:

export class SecondClass {
    someOtherVar:string;

    constructor(secondClass?:SecondClass) {
        someOtherVar='test';
    }
}

Would give me the error: ORIGINAL EXCEPTION: TypeError: second_class_1.default is not a constructor

Content of ./second-class

System.register([], function(exports_1, context_1) {
    "use strict";
    var __moduleName = context_1 && context_1.id;
    var SecondClass;
    return {
        setters:[],
        execute: function() {
            SecondClass = (function () {
                function SecondClass(secondClass) {
                    this.someOtherVar='test';
                }
                return SecondClass;
            }());
            exports_1("SecondClass", SecondClass);
        }
    }
});
//# sourceMappingURL=second-class.js.map

This is the compiled output from Typescript compiler

like image 378
Nandan Phadke Avatar asked Apr 24 '16 21:04

Nandan Phadke


People also ask

How do you fix not a constructor?

We tried to instantiate a value that is not a constructor as a constructor, which caused the error. To solve the "TypeError: 'X' is not a constructor" in JavaScript, make sure to only use the new operator on valid constructors, e.g. classes or constructor functions.

Is not a constructor in JavaScript?

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 you call a constructor in TypeScript?

In TypeScript, the constructor method is always defined with the name "constructor". In the above example, the Employee class includes a constructor with the parameters empcode and name . In the constructor, members of the class can be accessed using this keyword e.g. this. empCode or this.name .

How do I fix default is not a constructor in typescript?

To fix the ‘Error: *.default is not a constructor’ error with TypeScript, we should make sure the default export we’re importing is a class or constructor. export default class MapAction implements IMapAction { //... } to export the MapAction class as a default export with export default .

Which class constructor functions can be invoked in typescript?

Now TypeScript correctly tells you about which class constructor functions can be invoked - Derived can because it’s concrete, but Base cannot. In most cases, classes in TypeScript are compared structurally, the same as other types. For example, these two classes can be used in place of each other because they’re identical:

Why doesn't typescript detect constructor initializations?

TypeScript does not analyze methods you invoke from the constructor to detect initializations, because a derived class might override those methods and fail to initialize the members.

Why is there no static class in typescript?

TypeScript (and JavaScript) don’t have a construct called static class the same way C# and Java do. Those constructs only exist because those languages force all data and functions to be inside a class; because that restriction doesn’t exist in TypeScript, there’s no need for them.


1 Answers

There are some errors in the code :

  • missing {} from import

  • missing () from calling the constructor

  • missing this from accessing Class members

First Class code

import {SecondClass} from './second-class'

export class FirstClass {
    someVar:string;
    secondClass:SecondClass;

    constructor(firstClass?: FirstClass) {
        this.someVar='test';
        this.secondClass= new SecondClass();
    }
}

Second Class code:

export class SecondClass {
    someOtherVar:string;

    constructor(secondClass?:SecondClass) {
        this.someOtherVar='test';
    }
}
like image 95
tibbus Avatar answered Oct 13 '22 11:10

tibbus