11

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

4
  • Post content of ./second-class. Commented Apr 24, 2016 at 21:57
  • default is a javascript reserved word. Commented Apr 24, 2016 at 21:57
  • Possible cyclic error for SecondClass Commented Apr 24, 2016 at 21:59
  • I reverted your edit because it invalidates the answers and makes the question completely unhelpful for anyone coming along. Commented Apr 26, 2018 at 14:34

3 Answers 3

15

Error message implies that you used named export (export class SecondClass {}) in ./second-class (not default). So it means that your import should looks something like

import {SecondClass} from './second-class'
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. Modified the code but still getting error like ORIGINAL EXCEPTION: TypeError: second_class_1.SecondClass is not a constructor
Can you post contents of the './second-class'? (TypeScript, not transpiled).
I have it in my question just below the FirstClass code.
7

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';
    }
}

1 Comment

Thanks. These were typos when typing the question. The code, without these errors is giving said error.
0

this is too late, but I just got the same error right now. Solution is export SecondClass as default so Second Class code will be:

export default class SecondClass {
    someOtherVar:string;

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

and import in other class with import SecondClass from './second-class'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.