Skip to content
Advertisement

ES6 – is there an elegant way to import all named exports but not the default export?

I am looking for an elegant way to import all named exports without having to import the default as well.

In one file I am exporting many named constants plus a default:

JavaScript

In another file I would like to have an elegant way to import all named exports only, without having to import the default:

JavaScript

I do not want to:

JavaScript

because I do not need the default in anotherFile.js and my linting tools bug me about the defined but unused myDefault. Nor do I want to:

JavaScript

because that’s too much typing. I also do not want to object.omit the default:

JavaScript

Thanks for any help!

Advertisement

Answer

There is no separation between “named” and “default” exports. The default export is a named export, it just happens to have the name default which is special-cased for ease of use by certain syntax.

The only way to import all of the exported keys is with

JavaScript

and that will include the named export default if there is one. If you wish to exclude it from your checks, it would be up to you do handle that in your own logic, as you have done with your omit() example.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement