question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature] make generated codes from enum could be minified when not used

See original GitHub issue

Search 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

image

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:open
  • Created 5 years ago
  • Reactions:22
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
yordiscommented, Nov 7, 2021

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.

3reactions
slavafomincommented, May 7, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found