-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Unable to use enum
with const
modifier because of inline optimization
#21391
Comments
I would recommend using regular enums instead. |
@mhegazy I understand this about I feel like I'm taking crazy pills here since I am getting the impression that the TS devs promote the Of course I can drop the |
That is kinda what i said before, but this is what |
well.. i am telling you, it's "intended purpose" is to be removed and inlined, and not to be "constant". all enum values are constant by definition. |
Non-
Enum values are never mutable; this code is illegal regardless of enum X { Y }
X.Y = 3; If you want the enum object to be present at runtime for debugging purposes, use There are many different aspects to |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
IMO this is not about flags, this is about broken semantics of In TypeScript, +1 on reopening this, my current opinion is it’s a language design issue and the right solution is to ensure syntax used for compiler optimizations can’t be confused with syntax of high-level language features. Might be unlikely to change as it’s an entrenched feature by now. |
Our options are extremely limited because we're trying to not break existing code. Today the program
means "the expression statement If we were to try to co-opt the Prior to
was a syntax error. The only things we can write in place of |
It seems there is a design shortcoming with
const enum
in TS because there is no way to get a list of all values without changingconst enum
to a plain oldenum
. This is a problem because there are cases where a developer would want to use aconst enum
but can not because they can not programatically get a list of all possible values (due to the inlining optimization).In example suppose a developer wants to use a
const enum
to represent a field in a mongoose schema. Here is an example of what this code might look like:If this developer were to try and create a Mongoose schema, they would not be able to enforce strong types on this sex field they would fail because they would not have access to all the possible
const enum
values.The only way for me to get this to work is to remove the
const
declaration from the Sexenum
. That is not what I want to do because I have no intention of adding additional Sex values to thatenum
. Thus thisenum
should be able to be declared as aconst enum
but can not be because of the aforementioned reasons. This seems like a bug or an oversight. What should be possible here is for me to call some special reserved property on theenum
that will return a list of all keys for thatenum
. In example I would expect something like the following to work in the ideal scenario:This would allow me to use
const enum
types and also would allow me to know all possible values of theenum
itself. Another way might be to add a special keyword likevaluesof
to get them. So in code it would look likevaluesof Sex
which would return the array['Female', 'Male']
. Thoughts?The text was updated successfully, but these errors were encountered: