Skip to content

Rule proposal: no-enum / ban-enum / no-use-enum #561

Closed
@kachkaev

Description

@kachkaev

After switching to TypeScript, our team's monorepo ended up with quite a few enums in various parts of the code. Initially, referring to enums felt better than exporting named constants as we did in the good old JavaScript times (export const STATUS_READY = "ready"). However, we noticed a few issues with enums afterwards and decided to switch to TypeScript discriminated unions instead. The most irritating issue with enums for us was that their imports could cause cyclic module dependencies, thus magically resulting undefined in unexpected places, e.g. when running Jest tests.

We have removed all enums from the codebase, but there is still a chance that someone can introduce a new enum via a PR and this will be overlooked. I'm wondering if @typescript-eslint/eslint-plugin could have a rule named like no-enum / ban-enum / no-use-enum to prevent this from happening.

Example of incorrect code for this rule:

enum Direction {
  Up,
  Down,
  Left,
  Right,
}

Example of correct code for this rule:

type Direction = "up" | "down" | "left" | "right"

All the rule would have to do is searching for the enum token – that does not seem too complex. WDYT folks?

Related: #280 (see comment by @leoyli with a similar request)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions