Skip to content

noConstEnum

¥Summary

  • 规则生效日期:v1.0.0

    ¥Rule available since: v1.0.0

  • 诊断类别:lint/suspicious/noConstEnum

    ¥Diagnostic Category: lint/suspicious/noConstEnum

  • 此规则为推荐规则,默认启用。

    ¥This rule is recommended, which means is enabled by default.

  • 此规则包含 safe 修复程序。

    ¥This rule has a safe fix.

  • 此规则的默认严重级别为 warning

    ¥The default severity of this rule is warning.

¥How to configure

biome.json
{
"linter": {
"rules": {
"suspicious": {
"noConstEnum": "error"
}
}
}
}

¥Description

禁止 TypeScript const enum

¥Disallow TypeScript const enum

考虑使用 而不是对象。常量枚举是应该在使用站点内联的枚举。它们的使用可能导致导入不存在的值(因为 const 枚举被删除)。

¥Const enums are enums that should be inlined at use sites. Const enums are not supported by bundlers and are incompatible with the isolatedModules mode. Their use can lead to import nonexistent values (because const enums are erased).

因此,库作者和打包器用户不应使用 const 枚举。

¥Thus, library authors and bundler users should not use const enums.

¥Examples

¥Invalid

const enum Status {
Open,
Close,
}
code-block.ts:1:1 lint/suspicious/noConstEnum  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

The enum declaration should not be const

> 1 │ const enum Status {
^^^^^^^^^^^^^^^^^^^
> 2 │ Open,
> 3 │ Close,
> 4 │ }
^
5 │

Const enums are not supported by bundlers and are incompatible with the ‘isolatedModules’ mode. Their use can lead to import inexistent values.

See TypeScript Docs for more details.

Safe fix: Turn the const enum into a regular enum.

1 │ const·enum·Status·{
------

¥Valid

enum Status {
Open,
Close,
}

¥Related links