8

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

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

1
  • 4
    This question concerns syntax that was removed from TypeScript about 4 years ago. Commented Mar 7, 2016 at 15:09

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

4 Comments

It's interesting that section 9.2.2 of the spec states "It is not possible to export a module identifier declared in an import declaration." Unless I'm getting my terminology wrong, isn't that what we're doing above?
It doesn't work out of the box like this - but you can use it in definitions. i.e. declare module SomeModule { export import stuff = module("stuff"); }
NB. For anyone else stumbling on this now (like me), I can save you some time: this is purely historical and no longer appears in the example linked to by OP. Also import x = module became import x = require. (Was module(MyModule) ever valid? Not that it matters now...)
@DanielEarwicker indeed - back in 2012 we would have been using version 0.8 of TypeScript and the syntax was quite different.
4

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.

2 Comments

My understanding from the discussion on CodePlex (can't find link at this moment...) is that this is a bug, and export import is a temporary workaround.
I was using 0.8.1.1 ... Just upgraded to 0.8.3 and all my "export import" are now errors (can simply remove the export). So JcFx was right about it being a bug. Strange that Steve described a real use for this, what with that now?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.