如何将多个单个文件函数分组为 index.ts 中的命名和默认导出?

问题描述

问题

有多个文件,每个文件都导出一个函数作为认导出。

希望通过将它们作为命名导出从单个文件(例如 index.ts)中将它们进行分组,该文件将用作库 API,可以在其中根据需要访问函数

由于这些函数将在 JS/TS 项目中使用,因此可能需要将它们导出为命名导出和作为认导出的对象的一部分。

当前的解决方案冗长且重复,因为它涉及将每个函数作为别名导入,然后将它们导出并添加所需的函数名称,该名称也包含在认导出对象中:

// 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,// ...
};

问题

是否有更好的方法将这些单个文件函数分组并从 index.ts 导出,同时支持命名和认导出?

也欢迎提供有关文件结构和避免循环依赖的建议。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)