I encountered an issue in one of my TypeScript files where I have 2 classes, each with a constructor. Inside each class, it needs to obtain an instance of the other class. Here is the code snippet:
namespace pxsim {
export class Board extends pxsim.BaseBoard {
public tMap: trafficMap = new trafficMap();
public x: number;
public y: number;
constructor(){
this.y = 10;
this.x = this.tMap.x;
}
}
export class trafficMap{
public b: Board = new Board();
public y: number;
public x: number;
constructor(){
this.x = 20;
this.y = this.b.y;
}
}
}
Upon running the code in the browser, I encountered an error message stating
"trafficMap is not a constructor"
. It seems that the issue arises because in the Board
class, when attempting to create an instance of trafficMap
, the content of the latter has not been loaded yet. This leads me to believe that asynchronous constructors might be needed. Please let me know if there are any corrections to be made.