Skip to content

Commit 1af8dc0

Browse files
committed
docs(README): add more reason
1 parent 8101f2a commit 1af8dc0

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,20 @@ declare enum MyEnum { A: 0, ... }
4040
## Why?
4141

4242
Const enum can only works in the same file. It works by inlining the exact value into code.
43-
With [isolateModules](https://www.typescriptlang.org/tsconfig#isolatedModules), you can't use the exported const enum. The solution is to enable [preserveConstEnums](https://www.typescriptlang.org/tsconfig#preserveConstEnums) option to convert const enum to regular enum.
4443

45-
However, the regular enum compiles to
44+
```ts
45+
if (cond === MyEnum.A) { /*...*/ }
46+
```
47+
48+
will compile to the following code. That's a great inline optimization.
49+
50+
```ts
51+
if (cond === 0 /* A */) { /*...*/ }
52+
```
53+
54+
However, const enums only work in the same file with [isolateModules](https://www.typescriptlang.org/tsconfig#isolatedModules). Therefore, you can't use the exported const enum. The solution is to enable [preserveConstEnums](https://www.typescriptlang.org/tsconfig#preserveConstEnums) option to convert const enums to regular enums.
55+
56+
And the regular enum compiles to
4657

4758
```js
4859
export var MyEnum;
@@ -55,7 +66,7 @@ export var MyEnum;
5566
})(MyEnum || (MyEnum = {}));
5667
```
5768

58-
Not only can't you take advantage of enum inlining, but it also wastes a lot of bytes. That's the reason why this transform is made.
69+
which is verbose. Not only can't you take advantage of enum inlining, but it also wastes a lot of bytes. That's the reason why this transform is made.
5970

6071
# Installation
6172

0 commit comments

Comments
 (0)