Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 667
Oleg Mihailik Avatar asked Nov 17 '12 21:11

Oleg Mihailik


1 Answers

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.

like image 54
Fenton Avatar answered Oct 12 '22 12:10

Fenton