Oleg Mihailik
Oleg Mihailik

Reputation: 2590

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.

Upvotes: 8

Views: 7525

Answers (2)

Ken Smith
Ken Smith

Reputation: 20445

As of 0.8.1.1, you apparently need to use this syntax when you're exporting classes that extend classes declared in other modules.

For instance, in 0.8.1, you could say this:

import mUiBase = module("../UiBase");

export class BaseViewModel extends mUiBase.UiBase {
}

But in 0.8.1.1, that gives you an error "Exported class extends class from private module", so you need to do this instead:

export import mUiBase = module("../UiBase");

export class BaseViewModel extends mUiBase.UiBase {
}

Presumably that's intended, and not just a bug.

Upvotes: 4

Fenton
Fenton

Reputation: 250922

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.

Upvotes: 9

Related Questions