Skip to content

Don't emit const enums with reverse mapping (under preserveConstEnum) #37282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
4 of 5 tasks
timocov opened this issue Mar 8, 2020 · 2 comments
Open
4 of 5 tasks

Don't emit const enums with reverse mapping (under preserveConstEnum) #37282

timocov opened this issue Mar 8, 2020 · 2 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@timocov
Copy link
Contributor

timocov commented Mar 8, 2020

Search Terms

preserveConstEnums, const enum, enum

Suggestion

By default all const enums are preserved from output. One of the key differences of const enums is disallowing to looking up the reversed value:

enum Enum {
  Zero,
  One,
}

console.log(Enum[1]); // allowed, result is 'One'

const enum ConstEnum {
  Zero,
  One,
}

console.log(ConstEnum[1]); // Error

This means that we can't access const enum's "reversed" member even if preserveConstEnums flag is enabled.

JS code of const enum with enabled preserveConstEnums is pretty similar to just enums right now and contains string literals (even they couldn't be accessed from the code):

var ConstEnum;
(function (ConstEnum) {
    ConstEnum[ConstEnum["Zero"] = 0] = "Zero";
    ConstEnum[ConstEnum["One"] = 1] = "One";
})(ConstEnum || (ConstEnum = {}));

My feature request is change output of const enums when preserveConstEnums is enabled and strip "reversed" values from it:

var ConstEnum;
(function (ConstEnum) {
    ConstEnum["Zero"] = 0;
    ConstEnum["One"] = 1;
})(ConstEnum || (ConstEnum = {}));

Note: actually tsc already has similar behaviour if you specify string constant value for every const enum's member, but the values are strings, not numbers - it is kind of workaround if your const enum is used to declare string constants.

Use Cases

Reversed values for const enums are useless and cannot be accessed in the TS code, so why we should emit them? 🤔 I guess in this case the behaviour of const enums from types and from execution (JS) purposes will be the same.

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. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@RyanCavanaugh
Copy link
Member

Reversed values for const enums are useless and cannot be accessed in the TS code, so why we should emit them?

I mean, that's 100% true of const enum in general for both directions.

We created this flag because we found it very tiresome to do reverse lookups (from the value to the name) by hand in the debugger. I have to imagine other people are using it for the same reason and would be quite bothered by that scenario being broken.

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Mar 9, 2020
@RyanCavanaugh RyanCavanaugh changed the title Change output format of const enums if preserveConstEnums option is enabled Don't emit const enums with reverse mapping (under preserveConstEnum) Mar 9, 2020
@timocov
Copy link
Contributor Author

timocov commented Mar 9, 2020

Just as an use-case of using preserveConstEnum and const enums: inside the project we don't to use just enums because they are "massive" and aren't inlined so we use const enums everywhere. In other hand we provide a library, which could be used in JS code, so we don't want "break" their code and tell our consumers to use magic constants instead. That's why we've enabled preserveConstEnum - in the project we have inlined values, and in opposite we provide a way to use that enums in JS code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants