Back to Biomejs

noEnum

src/content/docs/linter/rules/no-enum.mdx

latest3.7 KB
Original Source

import { Tabs, TabItem } from '@astrojs/starlight/components';

<Tabs> <TabItem label="TypeScript and TSX" icon="seti:typescript"> ## Summary - Rule available since: `v1.9.0` - Diagnostic Category: [`lint/style/noEnum`](/reference/diagnostics#diagnostic-category) - This rule isn't recommended, so you need to enable it. - This rule doesn't have a fix. - The default severity of this rule is [**warning**](/reference/diagnostics#warning). ## How to configure ```json title="biome.json" { "linter": { "rules": { "style": { "noEnum": "error" } } } }
## Description
Disallow TypeScript enum.

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.

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

```ts
enum Foo {
    BAR = 'bar',
    BAZ = 'baz',
}
<pre class="language-text"><code class="language-text">code-block.ts:1:1 <a href="https://biomejs.dev/linter/rules/no-enum">lint/style/noEnum</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Orange;">⚠</span></strong> <span style="color: Orange;">Don't use </span><span style="color: Orange;"><strong>enum</strong></span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>enum Foo &#123; <strong> │ </strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong> BAR = 'bar', <strong><span style="color: Tomato;">&gt;</span></strong> <strong>3 │ </strong> BAZ = 'baz', <strong><span style="color: Tomato;">&gt;</span></strong> <strong>4 │ </strong>&#125; <strong> │ </strong><strong><span style="color: Tomato;">^</span></strong> <strong>5 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">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.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Use JavaScript objects or TypeScript unions instead.</span> </code></pre>

Valid

ts
const Foo = {
    BAR: 'bar',
    BAZ: 'baz',
} as const
ts
type Foo = 'bar' | 'baz'
ts
const enum Foo {
    BAR = 'bar',
    BAZ = 'baz',
}
</TabItem> </Tabs>