Follow

Does anyone know if it's possible to "merge" (import) the content of a JavaScript module into the current namespace? (Kind of like `using namespace` in C++.)

Basically, I would like to do `import * from 'foo.js'` where all the stuff in `foo.js` becomes directly available. All the options I see seem to force me to import stuff into a "new" namespace which I then have to prefix every time.

@amirrajan I don't want to specify which things to import, I want everything.

@lrz @amirrajan not possible without crazy hacks. you need to (1) use a namespace object or (2) specify all the imports yourself. it's by design so tooling can know which module an identifier comes from.

@lrz @judofyr outside of that you can pass in “self” and attach functions accordingly.

@lrz @amirrajan it helps if you use an editor (e.g. VS Code) which can insert imports automatically for you. then you just type "flatMap <TAB>" and it does The Right Thing™

@judofyr @amirrajan Do you use an extension for this? I can't seem to be able to do it with the vanilla config.

@lrz @amirrajan hmm. I'm using this in a TypeScript project, but it works in plain JS files as well. according to code.visualstudio.com/docs/lan it needs TS 2.6, but I'm not sure if it needs to be available locally or in the project

@lrz @amirrajan maybe you need a tsconfig.json/jsconfig.json?

@judofyr @amirrajan Indeed that was it! Works like a charm! Thanks again for your help and the pointers.

Sign in to participate in the conversation
mastodon.cloud

Generalistic and moderated instance.