Search code examples
javascripttypescriptes6-modules

Spread an object before exporting


I've got an object that's generated basing on another object. The object's type is fully known. It looks like this.

const mediaHooks = {
   sm: () => {}, //some func
   md: () => {}, //some func
};

Now, I would like to 'spread' that object before exporting, so its properties count as a top-level export and I can import them like:

import {sm} from './media';

Is it possible?


Solution

  • No, this is not technically possible.

    Instead, consider flipping your approach by using named exports and namespace imports

    // media-hooks.js
    export const sm = () => {}; //some func
    export const md = () => {}; //some func
    
    // app.js
    import * as mediaHooks from './media-hooks' ;
    import {sm} from './media-hooks';