ES6 module syntax: can I "export" as "Name from ..."?
See question. I found a great link for the available forms export
, but I didn't see what I was looking for.
Can you do something like the following?
// file: constants.js
export const SomeConstant1 = 'yay';
export const SomeConstant2 = 'yayayaya';
// file: index.js
export * as Constants from './constants.js';
those. this would provide a named export Constants
internally index.js
containing all named exports from module.js
.
This answer seems to be impossible in TypeScript; is the same true for pure JavaScript?
(This example is a bit contrived, in fact I'm trying to have a module prop-types.js
that uses named exports for internal use in a React package, but also exports prop type definitions PropTypes
for external consumption I've tried to simplify for the sake of the question.)
source to share
No, this is also not allowed in JS, however there is a suggestion to add it . For now, just use a two-step process with import to local and export, which:
// file: constants.js
export const SomeConstant1 = 'yay';
export const SomeConstant2 = 'yayayaya';
// file: index.js
import * as Constants from './constants.js';
export {Constants};
source to share
// file: index.js
// note, this doesn't have to be at the top, you can put it wherever you prefer
import * as AllExportsFromThisModule from "./index.js"; // point this at the SAME file
export default AllExportsFromThisModule;
export const SOME_CONSTANT = 'yay';
export const SOME_OTHER_CONSTANT = 'yayayaya';
source to share