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
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.
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.
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");
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 .
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 .
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:
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.
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.
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';
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With