I'm using TypeScript + AMD (RequireJs) and I'm facing a problem regarding circular dependency. tsc
deals with circular dependency pretty smooth but when it's run time, it took me hours to figure out that I'm facing a circular dependency problem.
Here's my scenario:
Singleton.ts
import Window = require("./Window");
class Singleton
{
private w : Window;
private counter : number = 0;
constructor() {
w = new Window();
}
Increment() : number {
return ++this.counter;
}
}
export = Singleton;
Window.ts
import Singleton = require("./Singleton");
declare var global_instance : Singleton;
class Window
{
private my_number : number = 0;
constructor() {
this.my_number = global_instance.Increment();
}
}
export = Window;
As simple as it seems, it leads to a circular dependency and thus I'm unable to implement it in a browser. But as I said tsc
deals with it perfectly without any problem. As I searched the Internet I found a couple of proposed solutions:
This one suggests adding a typeof
before the type to prevent compiler from actually putting the type's file in require list. But it results in a dozen of compiler errors so it's out.
This one sounds promising but it's a RequireJs solution and I'm not sure how to implement it in TypeScript!
Does anyone have any solution how to deal with circular dependency while benefiting from type checking of tsc
compiler?