Sunday, 25 October 2020

Import multiple named exports of module into a single object

Suppose I have:

animals.js

import cat from './cat';
import dog from './dog';
import emu from './emu';
import pig from './pig';
import cow from './cow';

export { cat, dog, emu, pig, cow };

In a module that uses animals.js, how can I import only a couple of needed ones into a keyed object? I'd like to be able to do something like:

my-module.js

import { cat, dog } as housePets from './animals';

// housePets == { cat: theCatModule, dog: theDogModule }

But according to my IDE, this syntax is not correct.

Is there a way to do this? Or is the best way to simply import all of them individually, then construct my own objects afterward?



from Import multiple named exports of module into a single object

No comments:

Post a Comment