{"id":10835,"library":"eslint-plugin-typescript-enum","title":"ESLint Rules for TypeScript Enums","description":"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`.\n\nThe 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.","status":"active","version":"2.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/shian15810/eslint-plugin-typescript-enum","tags":["javascript","typescript","eslint","typescript-eslint","enum","typescript-enum","plugin","eslint-plugin","rules"],"install":[{"cmd":"npm install eslint-plugin-typescript-enum","lang":"bash","label":"npm"},{"cmd":"yarn add eslint-plugin-typescript-enum","lang":"bash","label":"yarn"},{"cmd":"pnpm add eslint-plugin-typescript-enum","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency required for ESLint functionality.","package":"eslint","optional":false},{"reason":"Peer dependency required for TypeScript language services and AST parsing.","package":"typescript","optional":false},{"reason":"Peer dependency required to parse TypeScript code for ESLint.","package":"@typescript-eslint/parser","optional":false}],"imports":[{"note":"When configuring ESLint, use the short plugin name 'typescript-enum' without the 'eslint-plugin-' prefix.","wrong":"plugins: [\"eslint-plugin-typescript-enum\"]","symbol":"ESLint plugin configuration","correct":"plugins: [\"typescript-enum\"]"},{"note":"The 'recommended' configuration path requires the 'plugin:' prefix.","wrong":"extends: [\"typescript-enum/recommended\"]","symbol":"Recommended rule set","correct":"extends: [\"plugin:typescript-enum/recommended\"]"},{"note":"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.","wrong":"parser: \"babel-eslint\"","symbol":"TypeScript parser for ESLint","correct":"parser: \"@typescript-eslint/parser\""}],"quickstart":{"code":"module.exports = {\n  // Specifies the ESLint parser for TypeScript\n  parser: \"@typescript-eslint/parser\",\n  // Specifies the ESLint plugin to use\n  plugins: [\"typescript-enum\"],\n  // Extends with the recommended configuration from the plugin, disallowing enums\n  extends: [\"plugin:typescript-enum/recommended\"],\n  // Optional: Add other ESLint configurations as needed\n  parserOptions: {\n    ecmaVersion: 2020,\n    sourceType: \"module\",\n    // Required for rules that need type information\n    project: './tsconfig.json'\n  },\n  rules: {\n    // Additional custom rules or overrides if necessary\n  }\n};\n","lang":"javascript","description":"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."},"warnings":[{"fix":"Refactor existing TypeScript enums to use modern alternatives such as `const assertions`, `string unions`, or `discriminated unions` before enabling the recommended configuration.","message":"Enabling 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.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Favor `const assertions` on objects (`as const`) or `string unions` for type-safe constant sets, which are entirely compile-time constructs.","message":"TypeScript 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"Using `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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Review articles on TypeScript enum alternatives (e.g., `2ality.com/2020/02/enum-alternatives-typescript.html`) to understand safer and more robust patterns.","message":"TypeScript enums can have caveats and edge cases, with some aspects even considered not type-safe, leading to unexpected behavior at runtime or compile-time.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure `parser: \"@typescript-eslint/parser\"` is set in your `.eslintrc.js` and that `@typescript-eslint/parser` is installed as a dev dependency.","cause":"The ESLint parser is not correctly configured for TypeScript or is missing `@typescript-eslint/parser`.","error":"Parsing error: 'enum' keyword cannot be used."},{"fix":"Run `npm install --save-dev @typescript-eslint/parser` or `yarn add -D @typescript-eslint/parser` to install the parser.","cause":"The `@typescript-eslint/parser` package is not installed or ESLint cannot find it in your project's `node_modules`.","error":"Error: Failed to load parser '@typescript-eslint/parser' declared in '.eslintrc.js'."},{"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.","cause":"An enum declaration was found in your TypeScript code, and the `typescript-enum/no-enums` rule is enabled (e.g., via the recommended config).","error":"ESLint: 'Do not use TypeScript enums. Consider using union types, 'as const' objects, or discriminated unions instead.' (typescript-enum/no-enums)"}],"ecosystem":"npm"}