Search code examples
javascriptes6-module-loaderes6-modules

What type does `import * as Thing from '...'` make?


What type does import * as MyModule from 'module-name' create?

I'm assuming it's a normal javascript object with the keys being the names of the exported values but I don't know for sure.

Example:

./module-that-exports.js

export const a = 'foo';
export const b = 'bar';

./module-that-imports.js

import * as thing from './module-that-exports';

typeof thing; // what does this print? what is its type?

specifically, i'm using webpack. i don't know if that makes a difference.


Solution

  • Yes, it's an object (and not callable), so typeof will yield "object".

    However, it is not an ordinary object, it's an exotic module namespace object. It does not inherit from Object.prototype, it is not extensible, and all its properties are non-writable getters that resolve to the exported bindings. Your transpiler/module loader may not be able to completely emulate this though.