Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript wildcard import all module names into current namespace?

Tags:

javascript

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 );
like image 809
l00ser2410656 Avatar asked Oct 18 '22 13:10

l00ser2410656


2 Answers

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.

like image 110
Aluan Haddad Avatar answered Oct 21 '22 06:10

Aluan Haddad


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

like image 40
Jorawar Singh Avatar answered Oct 21 '22 07:10

Jorawar Singh