Skip to content

Commit d13a7ec

Browse files
committed
docs: improve messages
1 parent 372affb commit d13a7ec

File tree

5 files changed

+31
-19
lines changed

5 files changed

+31
-19
lines changed

README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Moreover, using enums in TypeScript has a lot of caveats and edge cases to keep
2121
- https://maxheiber.medium.com/alternatives-to-typescript-enums-50e4c16600b1
2222
- https://stackoverflow.com/questions/40275832/typescript-has-unions-so-are-enums-redundant/60041791#60041791
2323

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 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.
2525

2626
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.
2727

@@ -31,6 +31,12 @@ This article provides a very in-depth exploration on the alternatives to TypeScr
3131

3232
- https://2ality.com/2020/02/enum-alternatives-typescript.html
3333

34+
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+
3440
## Installation
3541

3642
First, install the peer dependencies:
@@ -61,7 +67,7 @@ module.exports = {
6167

6268
### Babel Config (Not Recommended)
6369

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:
6571

6672
```js
6773
module.exports = {
@@ -75,10 +81,10 @@ module.exports = {
7581

7682
**Key**: :heavy_check_mark: = recommended, :wrench: = fixable, :thought_balloon: = requires type information
7783

78-
| Name | Description | :heavy_check_mark: | :wrench: | :thought_balloon: |
79-
| -------------------------------------------------------------- | ------------------------------- | ------------------ | -------- | ----------------- |
80-
| [`typescript-enum/no-const-enum`](docs/rules/no-const-enum.md) | Disallow the use of const enums | | | |
81-
| [`typescript-enum/no-enum`](docs/rules/no-enum.md) | Disallow the use of all enums | :heavy_check_mark: | | |
84+
| Name | Description | :heavy_check_mark: | :wrench: | :thought_balloon: |
85+
| -------------------------------------------------------------- | -------------------------------------- | ------------------ | -------- | ----------------- |
86+
| [`typescript-enum/no-const-enum`](docs/rules/no-const-enum.md) | Disallow TypeScript `const` enums | | | |
87+
| [`typescript-enum/no-enum`](docs/rules/no-enum.md) | Disallow all types of TypeScript enums | :heavy_check_mark: | | |
8288

8389
## Contributing
8490

docs/rules/no-const-enum.md

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

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

55
## Rule Details
66

docs/rules/no-enum.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Disallow the use of all enums (`no-enum`)
22

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

55
## Rule Details
66

src/rules/no-const-enum.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import { createRule } from "../util";
22

3-
const noConstEnum = createRule({
3+
type MessageIds = "noConstEnum";
4+
type Options = [];
5+
6+
const noConstEnum = createRule<Options, MessageIds>({
47
name: "no-const-enum",
58
meta: {
69
type: "problem",
710
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,
1113
},
1214
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.",
1417
},
1518
schema: [],
1619
},
@@ -20,7 +23,7 @@ const noConstEnum = createRule({
2023
if (node.const === true) {
2124
context.report({
2225
node,
23-
messageId: "message",
26+
messageId: "noConstEnum",
2427
});
2528
}
2629
},

src/rules/no-enum.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import { createRule } from "../util";
22

3-
const noEnum = createRule({
3+
type MessageIds = "noEnum";
4+
type Options = [];
5+
6+
const noEnum = createRule<Options, MessageIds>({
47
name: "no-enum",
58
meta: {
69
type: "problem",
710
docs: {
8-
description: "Disallow the use of all enums",
9-
category: "Best Practices",
11+
description: "Disallow all types of TypeScript enums",
1012
recommended: "error",
1113
},
1214
messages: {
13-
message: "Unexpected enum.",
15+
noEnum:
16+
"In modern TypeScript, you may not need an enum when an object with `as const` could suffice.",
1417
},
1518
schema: [],
1619
},
@@ -19,7 +22,7 @@ const noEnum = createRule({
1922
TSEnumDeclaration: (node) => {
2023
context.report({
2124
node,
22-
messageId: "message",
25+
messageId: "noEnum",
2326
});
2427
},
2528
}),

0 commit comments

Comments
 (0)