I have two classes Foo and Bar. In class Bar I have a static variable called myFoo and I want it to be automatically initialized:
class Foo {
}
class Bar {
static myFoo: Foo = new Foo();
}
However, I'm getting this error:
Uncaught ReferenceError: Foo is not defined
If I initialize that static variable in Bar's constructor then it works fine:
class Bar {
static myFoo: Foo;
constructor() {
Bar.myFoo = new Foo();
}
}
Why is that? What did I do wrong when I tried to initialize the static variable myFoo directly?
static myFoo: Foo = new Foo();is indeed the correct way to initialize it. As explained in the accepted answer, the problem seems to be related to import order when Foo and Bar are in different files. (Just commenting in case anybody else is confused by the question.)