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
2 Answers
Reset to default 4import * 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
版权声明:本文标题:javascript - Typescript wildcard import all module names into current namespace? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741450907a2379480.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论