title: Export a TypeScript class from another file than it has been defined in
body: <p>
  For a while I've been trying to export the classes from a little library I wrote in a single place. I want each class do be agnostic of how they are made available to the outside but I can't get that to work so far.</p>
  
  <p>Let's assume I've got the class <code>Foo</code></p>
  
  <pre class="lang-js prettyprint-override"><code>// lib/foo.ts
  export class Foo {}
  </code></pre>
  
  <p>and the class <code>Bar</code></p>
  
  <pre class="lang-js prettyprint-override"><code>// lib/bar.ts
  export class Bar {}
  </code></pre>
  
  <p>Now I want to make those available to whoever uses the package via the <code>MyModule</code> namespace and this is where it gets tricky. I don't know how to export existing classes from a namespace defined in another place than the class itself.</p>
  
  <p>Obviously, this won't work because it's not valid TypeScript:</p>
  
  <pre class="lang-js prettyprint-override"><code>// api.ts
  import { Foo } from "./lib/foo";
  import { Bar } from "./lib/bar";
  
  export namespace MyModule {
      export Foo;
  
      export namespace MySubModule {
          export Bar;
      }
  }
  </code></pre>
  
  <p>I have found two potential solutions to this, but both do have their downsides. I can define a variable and assign the class to it:</p>
  
  <pre class="lang-js prettyprint-override"><code>// api.ts
  import { Foo as MyFoo } from "./lib/foo";
  import { Bar as MyBar } from "./lib/bar";
  
  export namespace MyModule {
      export const Foo = MyFoo;
  
      export namespace MySubModule {
          export const Bar = MyBar;
      }
  }
  </code></pre>
  
  <p>This will in fact allow me to create instances of the class from an external place like <code>new MyModule.MySubModule.Bar</code> but won't give me the possibility to use them as types since</p>
  
  <pre class="lang-js prettyprint-override"><code>let myBarInstance: MyModule.MySubModule.Bar;
  </code></pre>
  
  <p>will throw a TypeScript error stating that</p>
  
  <blockquote>
    <p>Module "path/to/module/api".MySubModule has no exported member 'Bar'</p>
  </blockquote>
  
  <p>The other way around I tried to use <code>export type Bar = MyBar</code> instead of <code>const</code> and while this works when saying</p>
  
  <pre class="lang-js prettyprint-override"><code>let myBarInstance: MyModule.MySubModule.Bar
  </code></pre>
  
  <p>on the other hand I naturally cannot instantiate <code>MyModule.MySubModule.Bar</code> since it's just a type alias.</p>
  
  <p>All of this goes the same way for the <code>Foo</code> class.</p>
  
  <p>Does anyone have any experience with this, have I maybe overlooked some other exporting feature TypeScript is providing?</p>