Skip to content

docs: [no-unsafe-enum-comparison] clarify motivation and applicability #10029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions packages/eslint-plugin/docs/rules/no-unsafe-enum-comparison.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import TabItem from '@theme/TabItem';
>
> See **https://typescript-eslint.io/rules/no-unsafe-enum-comparison** for documentation.
The TypeScript compiler can be surprisingly lenient when working with enums. String enums are widely considered to be safer than number enums, but even string enums have some pitfalls. For example, it is allowed to compare enum values against literals:
The TypeScript compiler can be surprisingly lenient when working with enums.
While overt safety problems with enums were [resolved in TypeScript 5.0](https://devblogs.microsoft.com/typescript/announcing-typescript-5-0/#all-enums-are-union-enums), some logical pitfalls remain permitted.
For example, it is allowed to compare enum values against non-enum values:

```ts
enum Vegetable {
Expand All @@ -21,7 +23,9 @@ declare const vegetable: Vegetable;
vegetable === 'asparagus'; // No error
```

The above code snippet should instead be written as `vegetable === Vegetable.Asparagus`. Allowing literals in comparisons subverts the point of using enums in the first place. By enforcing comparisons with properly typed enums:
The above code snippet should instead be written as `vegetable === Vegetable.Asparagus`.
Allowing non-enums in comparisons subverts the point of using enums in the first place.
By enforcing comparisons with properly typed enums:

- It makes a codebase more resilient to enum members changing values.
- It allows for code IDEs to use the "Rename Symbol" feature to quickly rename an enum.
Expand All @@ -39,17 +43,22 @@ enum Fruit {

declare let fruit: Fruit;

// bad - comparison between enum and explicit value instead of named enum member
fruit === 0;
```

```ts
enum Vegetable {
Asparagus = 'asparagus',
}

declare let vegetable: Vegetable;

// bad - comparison between enum and explicit value instead of named enum member
vegetable === 'asparagus';

declare let anyString: string;

// bad - comparison between enum and non-enum value
anyString === Vegetable.Asparagus;
```

</TabItem>
Expand All @@ -63,9 +72,7 @@ enum Fruit {
declare let fruit: Fruit;

fruit === Fruit.Apple;
```

```ts
enum Vegetable {
Asparagus = 'asparagus',
}
Expand All @@ -80,7 +87,12 @@ vegetable === Vegetable.Asparagus;

## When Not To Use It

If you don't mind number and/or literal string constants being compared against enums, you likely don't need this rule.
If you don't mind enums being treated as a namespaced bag of values, rather than opaque identifiers, you likely don't need this rule.

Sometimes, you may want to ingest a value from an API or user input, then use it as an enum throughout your application.
While validating the input, it may be appropriate to disable the rule.
Alternately, you might consider making use of a validation library like [Zod](https://zod.dev/?id=native-enums).
See further discussion of this topic in [#8557](https://github.com/typescript-eslint/typescript-eslint/issues/8557).

Separately, in the rare case of relying on an third party enums that are only imported as `type`s, it may be difficult to adhere to this rule.
Finally, in the rare case of relying on an third party enums that are only imported as `type`s, it may be difficult to adhere to this rule.
You might consider using [ESLint disable comments](https://eslint.org/docs/latest/use/configure/rules#using-configuration-comments-1) for those specific situations instead of completely disabling this rule.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy