[Feature] make generated codes from enum could be minified when not used
See original GitHub issueSearch Terms
For now TypeScript will transform enum from
enum Test {
Key = 1
}
to
var Test;
(function (Test) {
Test[Test["Key"] = 1] = "Key";
})(Test || (Test = {}));
This result is not friendly for uglyify or tree-shaking.
When Test
is not be used, the generated code block always remain as dead codes.
Suggestion
prefer to generate codes below:
var Test = /*#__PURE__*/(function () {
var e = {}
e[e["Key"] = 1] = "Key";
return e
})();
Examples
The suggestion version will be removed.
Checklist
My suggestion meets these guidelines:
- This wouldn’t be a breaking change in existing TypeScript / JavaScript code
- This wouldn’t change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn’t a runtime feature (e.g. new expression-level syntax)
Issue Analytics
- State:
- Created 5 years ago
- Reactions:22
- Comments:6 (2 by maintainers)
Top Results From Across the Web
How can I guarantee that my enums definition doesn't change ...
Those people would be right if they do not minify their code. However, no reasonable person would leave unminified code in the end...
Read more >Understanding the Performance Impact of Generated JavaScript
Using try-catch blocks to minify code requiring nested object access. Not everything can be minified or shortened though. Revisit the minified ...
Read more >Typescript Enums are bad!!1!!!1!!one - Are they really?
But this is true only because we are using Numeric Enums ( which are ... bundled and minified code loaded in the browser...
Read more >When using enums and R8…. Kotlin Vocabulary - Medium
The overhead and functionality of enums is essentially the same whether you're writing in Java or Kotlin code, but it's interesting what we...
Read more >Optimize Enums in TypeScript with "const" - Ultimate Courses
But Enums generate a lot of code, and by introducing the const keyword in TypeScript alongside our Enums, we can alleviate a lot...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey folks, any updates on this one?
We are changing most of the types to be union types because of tree-shaking issues, would appreciate some support on the topic.
This is indeed a very needed feature. We have a lot of packages and we are using enums extensively. Right now, all enums from all packages are getting added to the final application bundle, even if we are not using them at all, or using only a very small subset of all possible values.
I guess, one workaround would be to use constant enums instead, if possible.