admin管理员组

文章数量:1289529

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 );

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 );
Share Improve this question edited Mar 1, 2023 at 14:19 Aluan Haddad 31.9k10 gold badges83 silver badges95 bronze badges asked Apr 23, 2017 at 7:29 l00ser2410656l00ser2410656 1411 gold badge1 silver badge6 bronze badges 2
  • Why you do not want to import your module to a specific variables? Like so import {yourVariable} from "./myModule"; – FieryCod Commented Apr 23, 2017 at 7:47
  • 4 What you want is a global with equivalent, considered to be a bad practice – haim770 Commented Apr 23, 2017 at 7:47
Add a ment  | 

2 Answers 2

Reset to default 4
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 ment, this would be very much like the much maligned (and very rightly so) with statement.

It is simply not allowed.

Typescript wildcard import all module names into current namespace?

It is bad practice why?

Say you have couple hundreds modules in you MyModule and you want to import all think of how many unnecessary modules are going to import which you may will not use and that will take memory and make your app slow, You should import import the only module you want to work with.

e.g

import {A, D} from './MyModules';

If you still want to use wild card which will import all your modules

e.g

import * as http from "http"

then you should also use tree shaking so when deploying your app you can get rid off all other unnecessary modules.

Hope this helps

本文标签: javascriptTypescript wildcard import all module names into current namespaceStack Overflow