Skip to content

Commit 29f3cc7

Browse files
committed
Update readme
1 parent 6e8f0c6 commit 29f3cc7

File tree

3 files changed

+80
-4
lines changed

3 files changed

+80
-4
lines changed

README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,79 @@
11
# eslint-plugin-typescript-enum
22

33
ESLint rules for TypeScript enums.
4+
5+
## Motivations
6+
7+
From [TypeScript Handbook on Enums](https://www.typescriptlang.org/docs/handbook/enums.html):
8+
9+
> Enums are one of the few features TypeScript has which is not a type-level extension of JavaScript.
10+
11+
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](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals):
12+
13+
> Provide additional runtime functionality or libraries. Instead, use TypeScript to describe existing libraries.
14+
15+
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.
16+
17+
[Orta Therox](https://github.com/orta) from Typescript team mentioned in [one of his YouTube video](https://youtu.be/8qm49TyMUPI?t=1240) that the TypeScript team actually regrets some of the changes it made in the beginning, including introducing enums which basically add features to JavaScript.
18+
19+
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:
20+
21+
- https://maxheiber.medium.com/alternatives-to-typescript-enums-50e4c16600b1
22+
- https://stackoverflow.com/questions/40275832/typescript-has-unions-so-are-enums-redundant/60041791#60041791
23+
24+
Additionally, if you have been using [@babel/plugin-transform-typescript](https://babeljs.io/docs/en/babel-plugin-transform-typescript.html), you might already know that one of the caveats is to avoid using `const enum`s, as those require type information to compile.
25+
26+
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.
27+
28+
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.
29+
30+
This article provides a very in-depth exploration on the alternatives to TypeScript enums: https://2ality.com/2020/02/enum-alternatives-typescript.html
31+
32+
## Installation
33+
34+
First, install the required peer dependencies:
35+
36+
```sh
37+
npm install --save-dev eslint typescript @typescript-eslint/parser
38+
```
39+
40+
Next, install this package:
41+
42+
```sh
43+
npm install --save-dev eslint-plugin-typescript-enum
44+
```
45+
46+
## Usage
47+
48+
### Recommended Config
49+
50+
Configure this plugin in your ESLint configuration file (`.eslintrc.js`), this will disallow the use of enums altogether:
51+
52+
```js
53+
module.exports = {
54+
parser: "@typescript-eslint/parser",
55+
plugins: ["typescript-enum"],
56+
extends: ["typescript-enum/recommended"],
57+
};
58+
```
59+
60+
### Babel Config (Not Recommended)
61+
62+
For those that using [@babel/plugin-transform-typescript](https://babeljs.io/docs/en/babel-plugin-transform-typescript.html) and you want to allow the use of enums except `const enum`:
63+
64+
```js
65+
module.exports = {
66+
parser: "@typescript-eslint/parser",
67+
plugins: ["typescript-enum"],
68+
extends: ["typescript-enum/babel"],
69+
};
70+
```
71+
72+
## Rules
73+
74+
**Key**: :heavy_check_mark: = recommended, :wrench: = fixable, :thought_balloon: = requires type information
75+
76+
| Name | Description | :heavy_check_mark: | :wrench: | :thought_balloon: |
77+
| ---------------------------------------------------------------- | ------------------------------- | ------------------ | -------- | ----------------- |
78+
| [`typescript-enum/no-const-enum`](./docs/rules/no-const-enum.md) | Disallow the use of const enums | | | |
79+
| [`typescript-enum/no-enum`](./docs/rules/no-enum.md) | Disallow the use of enums | :heavy_check_mark: | | |

docs/rules/no-const-enum.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Disallow the use of TypeScript const enums (`no-const-enum`)
1+
# Disallow the use of const enums (`no-const-enum`)
22

3-
This rule disallows the use of TypeScript const enums.
3+
This rule disallows the use of `const enum`s.
44

55
## Rule Details
66

docs/rules/no-enum.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Disallow the use of TypeScript enums (`no-enum`)
1+
# Disallow the use of enums (`no-enum`)
22

3-
This rule disallows the use of TypeScript enums.
3+
This rule disallows the use of enums.
44

55
## Rule Details
66

0 commit comments

Comments
 (0)