Search code examples
javascriptes6-modules

Typescript wildcard import all module names into current namespace?


I want to have the ability to not have to type a module name (even an alias for a module) in front of all the places I am using a value from that module. (I'd rather not have to e.g. resort to running the C pre processor so I can do #include type stuff.)

(Close but no cigar: Angular 2 import statement wildcard syntax)

I want

import * from "./MyModule";
console.log( foobar );

instead of annoying stuff like:

console.log( MyModule.foobar )

or:

import * as M from "./MyModule";
console.log( M.foobar );

Solution

  • import * from "./MyModule";
    

    is not part of the ES Module specification. You cannot simply inject members into the scope without naming them. Identifier conflicts could arise whenever a module changed.

    As @haim770 points out in his comment, this would be very much like the much maligned (and very rightly so) with statement.

    It is simply not allowed.