eslint-plugin-typescript-enum provides ESLint rules specifically designed to manage and disallow the use of TypeScript enums within a project. The plugin operates on the premise that TypeScript enums, while a core language feature, introduce runtime representations that conflict with TypeScript's design goal of being a typed superset of JavaScript without adding runtime functionality. It highlights concerns such as potential type unsafety, caveats, and better modern alternatives like `const assertions`, `string unions`, and `discriminated unions`. The current stable version is 2.1.0, and the package appears to have an active release cadence, with several minor versions released recently. Its key differentiator is its explicit stance against TypeScript enums, aligning with a growing sentiment in the TypeScript community that discourages their use in favor of more JavaScript-native patterns. This makes it a critical tool for developers aiming to maintain a consistent, future-proof, and JavaScript-aligned codebase by enforcing alternatives.
npm install eslint-plugin-typescript-enumVerified import paths — ran on the pinned version, not inferred.
Configures ESLint to use the recommended rules of `eslint-plugin-typescript-enum`, effectively disallowing TypeScript enums in your project by applying the recommended rule set.
Refactor existing TypeScript enums to use modern alternatives such as `const assertions`, `string unions`, or `discriminated unions` before enabling the recommended configuration.
Favor `const assertions` on objects (`as const`) or `string unions` for type-safe constant sets, which are entirely compile-time constructs.
Avoid `const` enums when using Babel for TypeScript transformation. Instead, use literal types or `as const` objects that do not require type-aware transformations.
Review articles on TypeScript enum alternatives (e.g., `2ality.com/2020/02/enum-alternatives-typescript.html`) to understand safer and more robust patterns.
Ensure `parser: "@typescript-eslint/parser"` is set in your `.eslintrc.js` and that `@typescript-eslint/parser` is installed as a dev dependency.
Run `npm install --save-dev @typescript-eslint/parser` or `yarn add -D @typescript-eslint/parser` to install the parser.
Refactor the enum to use an alternative like a string union type (`type Status = 'active' | 'inactive';`), an `as const` object (`const Status = { ACTIVE: 'active' } as const;`), or a discriminated union.