Before jumping to conclusions, please take a moment to read the following:
In addition to TypeScript, my issue also involves Angular2.
Main Goal
I am in need of a method in app.component.ts that can take a string (Class Name) and generate an instance of that class. These classes are located in other ts files.
Scenario: I have a method
getWidgetObjectFromClassName(className : string) : Object{}
that is expected to return instances of the class name provided in string format.
However, there is a problem.
I attempted to use Namespace and tried
let instance = new SampleNS['OtherName']();
(SampleNS being a namespace), which worked flawlessly in a single file scenario.
The Issue:
Now that I have multiple ts files such as interfaces.ts, classes.ts, otherclasses.ts. I am utilizing export namespace SampleNS{}
in interface.ts, all functioning correctly. Next, in classes.ts, I am using
/// <reference path="interfaces.ts" />
and the same namespace SampleNS
.
Now, my method
getWidgetObjectFromClassName(className : string) : Object{}
resides in xyz.ts, and I am stuck on which import I should provide. The problem lies in the fact that I can only import the namespace from a single file, even though it is the same, thus limiting my ability to create instances to that specific file's namespace.
Plunker Link https://plnkr.co/edit/pt90F34KPsGoHDpSUQFD?p=preview