-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Suggestion: Accessing const enum member using variables with preserveConstEnums=true #13753
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
Comments
|
My intention is that our library code internally would use the enum (that is compiled to numeric constants). However the consumers of the library would be expected to either use the enum or string constants (that the library would convert to enum itself). This works very nicely with
transpiles to var Bar;
(function (Bar) {
var Foo;
(function (Foo) {
Foo[Foo["foo1"] = 1] = "foo1";
Foo[Foo["foo2"] = 2] = "foo2";
})(Foo = Bar.Foo || (Bar.Foo = {}));
var fk = Bar.Foo[1];
var f = Bar.Foo[fk];
})(Bar || (Bar = {})); |
That is not a supported use case of enums. they are either in all the way (enums) or inlined all the way (const enums). I suspect you are looking for an optimization for enum values along the lines of #6805 |
Yes, the goal is to inline the enum values. However, is there some detail I am not seeing that prevents using the |
That is not the indented design. the compiler makes assumptions about the emitted code based on this flag, and it is not possible to enforce this across different components. I believe what you are looking for is a regular enum, with an emit optimization to inline constants. again this is not supported at the moment. |
Thank you for the information! I suppose that we will use plain enums and once we get closer to release, we will probably try to modify the compiler to emit inline constants for all enums. |
Currently it is not possible to access
const enum
member at runtime by using string or number indexer. This makes sense because by default the declarations for these enums are not emitted in the JS code.However, we can use
preserveConstEnums:true
compiler option to both emit the enum declaration in the JS and to have the enum usages still be converted to simple numbers.As such, it would make sense that with
preserveConstEnums:true
the enum members should be accessible at runtime as well.The text was updated successfully, but these errors were encountered: