Skip to content

noEnum

诊断类别:lint/nursery/noEnum

¥Diagnostic Category: lint/nursery/noEnum

自从:v1.9.0

¥Since: v1.9.0

禁止 TypeScript 枚举。

¥Disallow TypeScript enum.

TypeScript 枚举不是 JavaScript 的类型级扩展,如类型注释或定义。用户可能希望禁用非类型级扩展以使用仅剥离类型的打包器或编译器。

¥TypeScript enums are not a type-level extension to JavaScript like type annotations or definitions. Users may wish to disable non-type-level extensions to use bundlers or compilers that only strip types.

此规则不涵盖常量枚举,因为 noConstEnum 已经处理它们。环境上下文中的枚举(包括声明文件)也将被忽略。

¥Const enums are not covered by this rule since noConstEnum already handles them. Enums within the ambient context, including declaration files, are ignores as well.

¥Examples

¥Invalid

enum Foo {
BAR = 'bar',
BAZ = 'baz',
}
code-block.ts:1:1 lint/nursery/noEnum ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Don’t use enum

> 1 │ enum Foo {
^^^^^^^^^^
> 2 │ BAR = ‘bar’,
> 3 │ BAZ = ‘baz’,
> 4 │ }
^
5 │

TypeScript enums are not a type-level extension to JavaScript like type annotations or definitions. Users may wish to disable non-type-level extensions to use bundlers or compilers that only strip types.

Use JavaScript objects or TypeScript unions instead.

¥Valid

const Foo = {
BAR: 'bar',
BAZ: 'baz',
} as const
type Foo = 'bar' | 'baz'
const enum Foo {
BAR = 'bar',
BAZ = 'baz',
}

¥Related links