Search code examples
typescriptobsolete

TypeScript – what is 'export import'?


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

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


Solution

  • 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.