Package Exports
- eslint-plugin-typescript-enum
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (eslint-plugin-typescript-enum) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
eslint-plugin-typescript-enum
ESLint rules for TypeScript enums.
Motivations
From TypeScript Handbook on Enums:
Enums are one of the few features TypeScript has which is not a type-level extension of JavaScript.
In other words, TypeScript enums have their corresponsing runtime representations, they are not erased from your emitted JavaScript files after being compiled. This conflicts with one of the non-goals of TypeScript Design Goals:
Provide additional runtime functionality or libraries. Instead, use TypeScript to describe existing libraries.
Having this TypeScript feature extending into your compiled JavaScript also conflicts with the TypeScript slogan of being a typed superset of JavaScript, which further introduces vendor lock-in.
Orta Therox from Typescript team mentioned in one of his YouTube video that the TypeScript team actually regrets some of the changes it made in the beginning, including introducing enums which basically add features to JavaScript.
Moreover, using enums in TypeScript has a lot of caveats and edge cases to keep in mind. Some aspects of it are even considered not type safe!!! Head over to these wonderful articles for more details on these issues:
- https://maxheiber.medium.com/alternatives-to-typescript-enums-50e4c16600b1
- https://stackoverflow.com/questions/40275832/typescript-has-unions-so-are-enums-redundant/60041791#60041791
Additionally, if you have been using @babel/plugin-transform-typescript, you might already notice that one of the caveats is to avoid using const enums, as those require type information to compile.
Nonetheless, TypeScript is still a very fantastic and powerful programming language that I love very much. Enums may have been a very good feature to have back in the early days (2011) when good alternatives did not yet exist.
However, we now already have alternatives better than enums, such as const assertions and string unions. Which is why I created this ESLint plugin to provide you with some configs and rules to disallow the use of TypeScript enums.
This article provides a very in-depth exploration on the alternatives to TypeScript enums: https://2ality.com/2020/02/enum-alternatives-typescript.html
Installation
First, install the required peer dependencies:
npm install --save-dev eslint typescript @typescript-eslint/parserNext, install this package:
npm install --save-dev eslint-plugin-typescript-enumUsage
Recommended Config
Configure this plugin in your ESLint configuration file (.eslintrc.js), this will disallow the use of enums altogether:
module.exports = {
parser: "@typescript-eslint/parser",
plugins: ["typescript-enum"],
extends: ["typescript-enum/recommended"],
};Babel Config (Not Recommended)
For those that using @babel/plugin-transform-typescript and you want to allow the use of enums except const enum:
module.exports = {
parser: "@typescript-eslint/parser",
plugins: ["typescript-enum"],
extends: ["typescript-enum/babel"],
};Rules
Key: ✔️ = recommended, 🔧 = fixable, 💭 = requires type information
| Name | Description | ✔️ | 🔧 | 💭 |
|---|---|---|---|---|
typescript-enum/no-const-enum |
Disallow the use of const enums | |||
typescript-enum/no-enum |
Disallow the use of enums | ✔️ |