Registry / testing / eslint-plugin-typescript-enum

eslint-plugin-typescript-enum

JSON →
library2.1.0jsnpmunverified

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-enum
INSTALL
IMPORT
SIG · ESLINT-PLUGIN-TYPE
E
eslint-plugin-typescript-enum
testingjavascriptv2.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ESLint plugin configuration
plugins: ["typescript-enum"]
plugins: ["eslint-plugin-typescript-enum"]
When configuring ESLint, use the short plugin name 'typescript-enum' without the 'eslint-plugin-' prefix.
Recommended rule set
extends: ["plugin:typescript-enum/recommended"]
extends: ["typescript-enum/recommended"]
The 'recommended' configuration path requires the 'plugin:' prefix.
TypeScript parser for ESLint
parser: "@typescript-eslint/parser"
parser: "babel-eslint"
This plugin requires `@typescript-eslint/parser` to correctly analyze TypeScript code. Other parsers for JavaScript or Babel may not provide the necessary type information or AST structure.

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.

module.exports = { // Specifies the ESLint parser for TypeScript parser: "@typescript-eslint/parser", // Specifies the ESLint plugin to use plugins: ["typescript-enum"], // Extends with the recommended configuration from the plugin, disallowing enums extends: ["plugin:typescript-enum/recommended"], // Optional: Add other ESLint configurations as needed parserOptions: { ecmaVersion: 2020, sourceType: "module", // Required for rules that need type information project: './tsconfig.json' }, rules: { // Additional custom rules or overrides if necessary } };
Debug
Known issues
breakingEnabling the `plugin:typescript-enum/recommended` configuration will disallow all uses of TypeScript enums. This is a breaking change for existing codebases that utilize enums and will result in ESLint errors until they are refactored.
fix
Refactor existing TypeScript enums to use modern alternatives such as `const assertions`, `string unions`, or `discriminated unions` before enabling the recommended configuration.
affects: >=1.0.0
gotchaTypeScript enums have runtime representations and are not erased from emitted JavaScript, which conflicts with TypeScript's design non-goal of not providing additional runtime functionality. This can lead to unexpected bundle sizes or runtime behavior.
fix
Favor `const assertions` on objects (`as const`) or `string unions` for type-safe constant sets, which are entirely compile-time constructs.
affects: >=1.0.0
gotchaUsing `const` enums with `@babel/plugin-transform-typescript` is problematic because `const` enums require type information to compile. Babel's TypeScript transform operates without type information.
fix
Avoid `const` enums when using Babel for TypeScript transformation. Instead, use literal types or `as const` objects that do not require type-aware transformations.
affects: >=1.0.0
gotchaTypeScript enums can have caveats and edge cases, with some aspects even considered not type-safe, leading to unexpected behavior at runtime or compile-time.
fix
Review articles on TypeScript enum alternatives (e.g., `2ality.com/2020/02/enum-alternatives-typescript.html`) to understand safer and more robust patterns.
affects: >=1.0.0
Errors
Common errors & fixes
Parsing error: 'enum' keyword cannot be used.
The ESLint parser is not correctly configured for TypeScript or is missing `@typescript-eslint/parser`.
fix
Ensure `parser: "@typescript-eslint/parser"` is set in your `.eslintrc.js` and that `@typescript-eslint/parser` is installed as a dev dependency.
Error: Failed to load parser '@typescript-eslint/parser' declared in '.eslintrc.js'.
The `@typescript-eslint/parser` package is not installed or ESLint cannot find it in your project's `node_modules`.
fix
Run `npm install --save-dev @typescript-eslint/parser` or `yarn add -D @typescript-eslint/parser` to install the parser.
ESLint: 'Do not use TypeScript enums. Consider using union types, 'as const' objects, or discriminated unions instead.' (typescript-enum/no-enums)
An enum declaration was found in your TypeScript code, and the `typescript-enum/no-enums` rule is enabled (e.g., via the recommended config).
fix
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.
Upgrade
Version history
2.1.0latest on npm
Audit
Dependencies
eslintrequiredPeer dependency required for ESLint functionality.
typescriptrequiredPeer dependency required for TypeScript language services and AST parsing.
@typescript-eslint/parserrequiredPeer dependency required to parse TypeScript code for ESLint.
Agent activity
2 hits · last 30 days
node
2
Resources
eslint-plugin-typescript-enum — npm install eslint-plugin-typescript-enum · libregistry