You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Additionally, if you have been using [@babel/plugin-transform-typescript](https://babeljs.io/docs/en/babel-plugin-transform-typescript.html), you might have already noticed that one of the [important caveat](https://babeljs.io/docs/en/babel-plugin-transform-typescript.html#caveats)s is to avoid using `const enum`s, as those require type information to compile.
24
+
Additionally, if you have been using [@babel/plugin-transform-typescript](https://babeljs.io/docs/en/babel-plugin-transform-typescript.html), you might have already noticed that one of the [important caveat](https://babeljs.io/docs/en/babel-plugin-transform-typescript.html#caveats)s is to avoid using `const` enums, as those require type information to compile.
25
25
26
26
Nonetheless, TypeScript is undoubtedly a very fantastic programming language with an extremely powerful type system. Enums may have been a very good feature to have back in the early days (2011) when good alternatives did not yet exist.
27
27
@@ -31,6 +31,12 @@ This article provides a very in-depth exploration on the alternatives to TypeScr
Last but not least, as stated in [Objects vs Enums section of TypeScript Handbook on Enums](https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums):
35
+
36
+
> In modern TypeScript, you may not need an enum when an object with as const could suffice.
37
+
>
38
+
> The biggest argument in favour of this format over TypeScript’s enum is that it keeps your codebase aligned with the state of JavaScript, and when/if enums are added to JavaScript then you can move to the additional syntax.
39
+
34
40
## Installation
35
41
36
42
First, install the peer dependencies:
@@ -61,7 +67,7 @@ module.exports = {
61
67
62
68
### Babel Config (Not Recommended)
63
69
64
-
If you are using [@babel/plugin-transform-typescript](https://babeljs.io/docs/en/babel-plugin-transform-typescript.html) and want to allow the general use of enums except `const enum`s:
70
+
If you are using [@babel/plugin-transform-typescript](https://babeljs.io/docs/en/babel-plugin-transform-typescript.html) and want to allow the general use of enums except `const` enums:
65
71
66
72
```js
67
73
module.exports= {
@@ -75,10 +81,10 @@ module.exports = {
75
81
76
82
**Key**: :heavy_check_mark: = recommended, :wrench: = fixable, :thought_balloon: = requires type information
77
83
78
-
| Name | Description |:heavy_check_mark:|:wrench:|:thought_balloon:|
Copy file name to clipboardExpand all lines: src/rules/no-const-enum.ts
+9-6Lines changed: 9 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,19 @@
1
1
import{createRule}from"../util";
2
2
3
-
constnoConstEnum=createRule({
3
+
typeMessageIds="noConstEnum";
4
+
typeOptions=[];
5
+
6
+
constnoConstEnum=createRule<Options,MessageIds>({
4
7
name: "no-const-enum",
5
8
meta: {
6
9
type: "problem",
7
10
docs: {
8
-
description: "Disallow the use of const enums",
9
-
category: "Best Practices",
10
-
recommended: "error",
11
+
description: "Disallow TypeScript `const` enums",
12
+
recommended: false,
11
13
},
12
14
messages: {
13
-
message: "Unexpected const enum.",
15
+
noConstEnum:
16
+
"Unexpected `const` enum, use regular enum instead. As a side note, in modern TypeScript, you may not need an enum when an object with `as const` could suffice.",
0 commit comments