When working with the module foo, calling bar.factoryMethod('Blue')
will result in an instance of WidgetBlue
.
module foo {
export class bar {
factoryMethod(classname: string): WidgetBase {
return new foo["Widget" + classname]();
}
}
export class WidgetBase {
A: number;
B: string;
}
export class WidgetBlue extends WidgetBase { }
export class WidgetRed extends WidgetBase { }
}
The purpose of this structure is to facilitate plug-in widgets.
However, when a plug-in like WidgetGreen
is declared in a different file, issues arise quickly.
module foo {
export class WidgetGreen extends WidgetBase { }
}
Surprisingly, WidgetGreen
seems to be out of scope during run-time within factoryMethod
. The issue can be resolved by moving it to the same file, but this defeats the purpose of having plug-in classes in separate files.
This question suggests that the code should work as intended, but it fails. Why is this happening and what can be done about it?