如何通过中间 TS 文件导入枚举?

How can I import an enum through an intermediate TS file?

我文件A 我有:

export enum MyFluffyEnum { Beauty, Courage, Love }

在文件 B 中我有:

import { MyFluffyEnum } from "./A";

export type { MyFluffyEnum };

在文件 C 中我有:

import { MyFluffyEnum } from "./B";

编译时错误:

'MyFluffyEnum' cannot be used as a value because it was exported using 'export type'.

如果我在文件 B(中间文件)中放置一个单独的 export { MyFluffyEnum };,我会收到错误消息“找不到模块:无法解析 'my-project/path/here' 中的 'B.tsx'”。

如果我直接导​​入enum为named export的文件,就没有错误了。

可能相关的链接:

  1. https://github.com/dividab/tsconfig-paths-webpack-plugin/issues/78

解决了问题:

I was having a WebPack config file that was not actually used. I was actually using CRA. So I started using react-app-rewired and WebPack v4 as an intermediate solution till I start using "pure" WebPack. In config-overrides.js I put:

require("tsconfig-paths-webpack-plugin");

module.exports = {
  webpack: function(config, env) {
    return {
      ...config,
      resolve: {
        ...config.resolve,
        plugins: [
          ...config.resolve.plugins,
          new TsconfigPathsPlugin({
            extensions: [".js", ".jsx", ".ts", ".tsx"],
          }),
        ],
      },
    };
  },
};

After migrating to react-app-rewired and making this change, the Failed to compile. error is gone. This also solves the issue in .