Back to Biomejs

useConsistentEnumValueType

src/content/docs/linter/rules/use-consistent-enum-value-type.mdx

latest5.3 KB
Original Source

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

<Tabs> <TabItem label="TypeScript and TSX" icon="seti:typescript"> :::caution This rule is part of the [nursery](/linter/#nursery) group. This means that it is experimental and the behavior can change at any time. ::: :::note This rule belongs to the types domain. This means that its activation will activate the Biome Scanner to scan the files of your project, and enable the type inference engine. Read more about it in the [documentation page](/linter/domains#types) ::: ## Summary - Rule available since: `v2.3.13` - Diagnostic Category: [`lint/nursery/useConsistentEnumValueType`](/reference/diagnostics#diagnostic-category) - This rule doesn't have a fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - This rule belongs to the following domains: - [`types`](/linter/domains#types) - Sources: - Same as [`@typescript-eslint/no-mixed-enums`](https://typescript-eslint.io/rules/no-mixed-enums)

How to configure

json
{
	"linter": {
		"rules": {
			"nursery": {
				"useConsistentEnumValueType": "error"
			}
		}
	}
}

Description

Disallow enums from having both number and string members.

TypeScript enums are allowed to assign numeric or string values to their members. Most enums contain either all numbers or all strings, but in theory you can mix-and-match within the same enum. Mixing enum member types is generally considered confusing and a bad practice.

Examples

Invalid

ts
enum Status {
  Unknown,
  Closed = 1,
  Open = 'open',
}
<pre class="language-text"><code class="language-text">code-block.ts:3:3 <a href="https://biomejs.dev/linter/rules/use-consistent-enum-value-type">lint/nursery/useConsistentEnumValueType</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Inconsistent enum value type.</span> <strong>1 │ </strong>enum Status &#123; <strong>2 │ </strong> Unknown, <strong><span style="color: Tomato;">&gt;</span></strong> <strong>3 │ </strong> Closed = 1, <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>4 │ </strong> Open = 'open', <strong>5 │ </strong>&#125; <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Another inconsistent enum value type.</span> <strong>2 │ </strong> Unknown, <strong>3 │ </strong> Closed = 1, <strong><span style="color: Tomato;">&gt;</span></strong> <strong>4 │ </strong> Open = 'open', <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;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>5 │ </strong>&#125; <strong>6 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Mixing number and string enums can be confusing. Make sure to use a consistent value type within your enum.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit </span><span style="color: lightgreen;"><a href="https://biomejs.dev/linter/#nursery">https://biomejs.dev/linter/#nursery</a></span><span style="color: lightgreen;"> for more information.</span> </code></pre>

Valid

ts
enum Status {
  Unknown = 0,
  Closed = 1,
  Open = 2,
}
ts
enum Status {
  Unknown,
  Closed,
  Open,
}
ts
enum Status {
  Unknown = 'unknown',
  Closed = 'closed',
  Open = 'open',
}
</TabItem> </Tabs>