有没有办法在Typescript中导出带有ES6设置的枚举?

Is there a way to export enums with ES6 settings in Typescript?

本文关键字:ES6 设置 枚举 Typescript 有没有      更新时间:2023-09-26

在使用Typescript时,我试图使用ES6导入/导出语句将enum导出到另一个文件中,如下所示:

export enum EnumerableThing {
  green,
  red,
  blue
}

然而,当我试图用import { EnumerableThing } from './enum'导入该语句时,它没有被检测到,也不影响生成的Javascript代码。

这是为什么/如何修复它的原因?

要注意,我在我的tsconfig中有ES6导入语句。我意识到接口实际上也不编译Javascript代码,然而,接口是可导出的,尽管没有被编译成Javascript本身。

编辑,这是我的tsconfig文件
{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "declaration": false,
        "noImplicitAny": false,
        "removeComments": true,
        "noLib": false,
        "jsx": "react",
        "allowJs": true
    },
    "exclude": [
      "node_modules"
    ],
    "compileOnSave": false
}

提前感谢!

如果您不实际使用导入,它将不会出现在生成的JS中。如果引用 EnumerableThing,例如let x = EnumerableThing.green,则将保留导入