-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Enum unification and improvements #50528
Conversation
@typescript-bot pack this |
Heya @RyanCavanaugh, I've started to run the tarball bundle task on this PR at 50a952b. You can monitor the build here. |
@typescript-bot test this |
Heya @ahejlsberg, I've started to run the extended test suite on this PR at 50a952b. You can monitor the build here. |
Heya @ahejlsberg, I've started to run the abridged perf test suite on this PR at 50a952b. You can monitor the build here. Update: The results are in! |
Heya @ahejlsberg, I've started to run the parallelized Definitely Typed test suite on this PR at 50a952b. You can monitor the build here. |
Heya @ahejlsberg, I've started to run the diff-based user code test suite on this PR at 50a952b. You can monitor the build here. Update: The results are in! |
@ahejlsberg Here they are:Comparison Report - main..50528
System
Hosts
Scenarios
Developer Information: |
@ahejlsberg Here are the results of running the user test suite comparing Everything looks good! |
Heya @ahejlsberg, I've run the RWC suite on this PR - assuming you're on the TS core team, you can view the resulting diff here. |
Hey @RyanCavanaugh, I've packed this into an installable tgz. You can install it for testing by referencing it in your
and then running There is also a playground for this build and an npm module you can use via |
@typescript-bot test this |
Heya @ahejlsberg, I've started to run the diff-based top-repos suite on this PR at 3f66f6c. You can monitor the build here. Update: The results are in! |
Is this included in a published version of Typescript yet? Based on the comments in this PR thread, it sounds like it should be in Typescript 5. I'm on Typescript 5.0.2, but getting the error "const enum member initializers can only contain literal values and other computed enum values.", which I believe shouldn't happen if I'm on a version of typescript that includes this code? I'm copying the example from the PR:
|
@Sweater-Baron Yes it is, your example is working perfectly fine running version 5.0.2 (see playground). If you use VSCode make sure that you use your local TypeScript version and not VSCode's version. |
Is there any plan to implement enum extension like in this playground (or some other way to extend enums)?: |
I have set my vscode to use 5.0.2, but im still getting the error |
@JordanDC2 Not 100% exactly what you asked for.. |
@WORMSS yeah thats kind of what im having to work with currently, i just wish it was a capability of Enums since we can have computed values now but not computed keys |
Man, you saved me, thank you. |
Sounds weird, but I'm on Typescript 5.0.2. I'm forcing VSC to use the local installed Typescript version (which it recognizes is 5.0.2 instead of VSC's 4.9.5) and the enum values still get udnerlined with red squiggly line.
Any ideas what I'm missing here? I see the playground link posted above by @janbiasi and it should be working for 5.0.2. But it's not. I confirm that the project has 5.0.2 installed with |
We just tried to update our code base and we noticed a ton of errors. Turns out this change basically means you cannot treat an enum as basically "just" a number anymore? No way to optionally tell TypeScript to allow "optional" values that are basically null? This is somewhat weird, because you now can't have a fall back state and HAVE to define a "NONE" type.
This now is an error. Was there any reason not to allow for a fallback here? |
how come would be really useful to not have this not so obvious difference you can check some examples here |
@gabriel-calin-lazar See my comment here. |
@tka85 any ideas about it? Also have TS above 5 and it complains |
@vtarelkin Nope. No response. |
An old PR is really not the best place to ask for help or report bugs. I would really suggest filing an issue if you want it to be seen by the right people. |
@jakebailey people are searching over the whole net and hope to find a clue here) maybe you could help us please?)) I have a TS above 5th version and it still complains |
Please, please, please, file an issue and fill out the template. |
@jakebailey this issues comes the first google search result. I have typescript 5.1.3 and just noticed the same problem, still trying to find any relevant tickets in this repo. If there is a solution - it might be helpful to add it here, or at least have a link somewhere in comments |
Please file a new issue outlining the problem you're having |
Since #9407 TypeScript has had two kinds of enum types: Numeric enum types and literal enum types (also known as union enum types). Numeric enum types have permitted enum members to have computed values, but not literal types; whereas literal enum types have required members to be declared using simple numeric or string literals only. The distinction between these two kinds of enums is subtle and has been the source of a fair amount of confusion over time.
With this PR we unify the two kinds into a single hybrid that incorporates the best of both worlds. Specifically, enums are always represented as unions of their member types, member values may be computed by constant or non-constant expressions, and members with constant values are given literal types. The PR only affects type checking of enums. There are no changes to the emitted code (except for better inlining of constant expressions in some cases).
An enum declaration now declares a type representing the entire enum and types representing each member (similar to literal enum types). For example,
declares a type
E
and typesE.A
,E.B
, andE.C
, whereE
is the unionE.A | E.B | E.C
.An enum member has a literal enum type when it is initialized by a constant expression or when it has no initializer and is given an auto-incremented numeric value. In the example above,
E.A
andE.B
denote enum literal types with values100
and"foo"
respectively.An enum member has a unique and opaque enum member type when it is initialized by a non-constant expression. Non-constant initializer expressions must be assignable to type
number
and are not permitted inconst
enum declarations. In the example above,E.C
denotes a unique enum member type representing the value computed by the non-constant initializer expression. This unique type is assignable to typenumber
, but otherwise incompatible with other types.An expression is considered a constant expression if it is
+
,-
, or~
applied to a numeric constant expression,+
,-
,*
,/
,%
,**
,<<
,>>
,>>>
,|
,&
,^
applied to two numeric constant expressions,+
applied to two constant expressions whereof at least one is a string,x.y.z
) that references aconst
variable with a constant expression initializer and no type annotation,x.y["z"]
) that references an enum member with an enum literal type.Note that constant expressions are not permitted to make forward references--any value referenced by a constant expression must have been previously declared.
Some examples:
NOTE: This PR is technically a breaking change because checking of the new unified enum types is stricter than the old numeric enum types.
Fixes #27976.
Fixes #35875.
Fixes #40793.
Fixes #43902.