1

Problem

Have several files each exporting a single function as a default export.

Want to group these via exporting them as named exports from a single file like index.ts that will serve as a library API where functions can be accessed as needed.

Since these functions will be used in JS/TS projects, they may be a need to export them as named exports and as part of an object that serves as the default export.

The current solution is verbose and repetitive as it involves importing each function as an alias, then exporting them add the desired function name, which is also contained within the default exported object:

// index.ts
import { default as _add } from './add';
import { default as _multiply } from './multiply';
// ...

// `export const add = _add;` and `export add = _add;` both fail due to incorrect declaration.

export const add = _add; // Used in another file, e.g. in `test.ts`: `import { add } from './index'; add(1);`.
export const multiply = _multiply;
// ...

export default { // Used in another file, e.g. in `test.ts`: `import helpers from './index'; helpers.add(1);`.
    add,
    multiply,
    // ...
};

Question

Is there a better way to group these single file functions and export them from index.ts whilst supporting named and default exports?

Advice regarding file structure and avoiding circular dependencies is also welcomed.

surajs02
  • 451
  • 7
  • 18

0 Answers0