There is a specific thing i want to do from time to time, that i cannot figure out:
suppose module1.js
exports 3 values:
//module1.js
export const a = 'a';
export const b = 'b';
export const c = 'c';
And then, in module2.js
, I want to import two of these, into an object (as a kind of namespace thing):
//module2.js
import { a, c } as constants from './module1'; //<-WRONG!
So what I end up doing is something like this:
//module2.js
import { a, c } from './module1';
const constants = { a, c };
This works, but now a
and c
exist both in constants
and also directly in the module scope.
Is there a way to avoid this?
As per MDN documentation, you can either set an alias on entire module contents such as
* as constants
or a single content such asb as constants
. But you can't set an alias on specific contents. So one of the solutions would be using *.Another possible solution would be passing
{ a, c }
as default.Lastly, If you don't want to pass these constants as default, you can create an object and pass that object.
Do you mean something like
You could also remove some if you need to pass them down, something like lodash pick