TypeScript – what is 'export import'?

Oleg Mihailik picture Oleg Mihailik · Nov 17, 2012 · Viewed 7.1k times · Source

Apparently, you can say 'export import xx = module("xx")' in TypeScript.

But what does that mean? I didn't see that in the spec.

Answer

Fenton picture Fenton · Nov 17, 2012

Good observation.

This is a composition technique that makes the entire imported module act like an external module created within the enclosing module. Here is a shortened example:

module MyModule {
    export class MyClass {
        doSomething() {

        }
    }
}

declare module EnclosingModule {
    export import x = module(MyModule);
}

var y = new EnclosingModule.x.MyClass();

The export keyword on its own makes a module an external module. In this case, it is making MyModule an external module of the enclosing module even though it isn't originally defined inside of the enclosing module.

Why?

I guess this is a handy way of re-using modules rather than repeating them in different contexts - making them accessible in more than one place where it seems logical to do so.