title: TypeScript: Factory pattern to use dynamic imports is not allowing to construct new instance of the object for composition
body: <p>I am implementing factory pattern in Typescript with dynamic imports so that I can initialize the load, initialize (with necessary composition) of the demanded module on run-time.</p>
  
  <p>I am able to load the module dynamically on demand as <a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-4.html" rel="nofollow noreferrer">https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-4.html</a></p>
  
  <p>However it is not allowing me to initialize the loaded module.
  Though in developer console I can do the initialization and even compose it seamlessly by initializing its sub-modules and classes composed in the module.</p>
  
  <p>Struggling a lot to find this solution and tried out many things but didn't get it solved. In C# we can do it using reflection and create instance of libraries and classes as lazy loading without getting direct reference to them.</p>
  
  <p>The error I am getting is "// Cannot use 'new' with an expression whose type lacks a call or construct signature.ts(2351)" when I am doing  </p>
  
  <pre><code>let y: Interfaces.IComponent =  new comp(); 
  </code></pre>
  
  <p>to construct it and assign it to varebale with type of interface of the that object is implementing.</p>
  
  <p>same for the parent class type that component is extending</p>
  
  <pre><code>let x: ComponentImpl =  new comp();
  </code></pre>
  
  <p>Please have a look at the code of typescript below.</p>
  
  <pre><code>    import { Interfaces } from 'shared';
      import { ComponentImpl } from 'core';
  
      export default class Factory {
          private _loadedModules: Map&lt;string, Interfaces.IComponent&gt; = new Map&lt;string, Interfaces.IComponent&gt;();
          private static _instace: Factory;
  
          private constructor() {
  
          }
  
          public static get Instance(): Factory {
              return this._instace || (this._instace = new this());
          }
  
          public getComponent(component: string): Promise&lt;Interfaces.IComponent&gt; {
              return new Promise&lt;Interfaces.IComponent&gt;(async (resolve, reject) =&gt; {
                  let comp = this._loadedModules.get(component);
                  if (comp !== null) {
                      comp = await import(`./${component}`);
                      if (comp) {
  
  // ----------------------------------------------------------------------------
                          // ** NOTE: On run time I can see the module is loaded corrctly and I can initialze its sub classes in developer console.
                          // like controller = new comp.controller(); (get console log from constructor)
                          // controller.sayHello();
                          // controller.setAPIInstance(new comp.getAPI());
                          // controller.saveToAPI();
                          let y: Interfaces.IComponent =  new comp(); // Cannot use 'new' with an expression whose type lacks a call or construct signature.ts(2351)
                          let x: ComponentImpl =  new comp(); // Cannot use 'new' with an expression whose type lacks a call or construct signature.ts(2351)
                          this._loadedModules.set(component, comp);
  
  
                          resolve(comp);
                      } else {
                          reject("Unable lo load module");
                      }
                  } else {
                      setTimeout(() =&gt; {
                          resolve(comp);
                      }, 1);
                  }
  
              });
          }
      }
  </code></pre>