-
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
Bitwise enum initializer: compile-time constant but not allowed as a type reference? #22709
Comments
Seems like it should be allowed. @DanielRosenwasser ? |
My understanding is we don't have member types for any types of binary operations. From ze handbook
It's not clear to me right now why we have this specific subset in mind; maybe there was a reason, but I can't recall what might stop us from just making an enum member type for every enum member whose containing enum solely holds computed values. |
I tried to track down the commit, it seems to be this one: f7753af Although I don't see the actual changes in that commit, but searching for |
See comments in #26241 |
TypeScript Version: 2.7.2
Search Terms: const enum bitwise initializer
I'm trying to have "flags" as a type on interfaces, so that bitwise operators can be used to distinguish between various types and adjust intellisense accordingly. This works perfectly for enums and const enums where all values are literals.
The compiler won't allow it for constant values that are not literals (e.g. bitwise expressions).
It seems like an unnecessary restriction since constant expressions can easily be converted to literals anyway (even intellisense shows them as literals when you mouse over) and they can also be used in
const enum
.The most relevant information I could find was this SO question's answer: https://stackoverflow.com/questions/28818849/how-do-the-different-enum-variants-work-in-typescript/
However that also does not address the distinction between non-literal constants and literals.
I can see where the check happens (
checker.ts
, line 20496):But no clarification/justification on why. See a small example below:
Code
Expected behavior:
I would expect compile-time constants and literals to have the same kinds of restrictions because they evaluate to the same thing.
So either:
Actual behavior:
Intellisense shows the computed value of a bitwise expression and bitwise initializers are allowed in const enum, but they cannot be used as a type reference.
Related Issues:
#1029
#202
The text was updated successfully, but these errors were encountered: