- Notifications
You must be signed in to change notification settings - Fork12.9k
Enum unification and improvements#50528
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
@typescript-bot pack this |
typescript-bot commentedAug 29, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Heya@RyanCavanaugh, I've started to run the tarball bundle task on this PR at50a952b. You can monitor the buildhere. |
@typescript-bot test this |
typescript-bot commentedAug 29, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Heya@ahejlsberg, I've started to run the extended test suite on this PR at50a952b. You can monitor the buildhere. |
typescript-bot commentedAug 29, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Heya@ahejlsberg, I've started to run the abridged perf test suite on this PR at50a952b. You can monitor the buildhere. Update:The results are in! |
typescript-bot commentedAug 29, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Heya@ahejlsberg, I've started to run the parallelized Definitely Typed test suite on this PR at50a952b. You can monitor the buildhere. |
typescript-bot commentedAug 29, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Heya@ahejlsberg, I've started to run the diff-based user code test suite on this PR at50a952b. You can monitor the buildhere. 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 diffhere. |
typescript-bot commentedAug 29, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Hey@RyanCavanaugh, I've packed this intoan installable tgz. You can install it for testing by referencing it in your
and then running There is also a playgroundfor this build and annpm module you can use via |
@typescript-bot test this |
typescript-bot commentedOct 17, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Heya@ahejlsberg, I've started to run the diff-based top-repos suite on this PR at3f66f6c. You can monitor the buildhere. Update:The results are in! |
typescript-bot commentedOct 17, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Heya@ahejlsberg, I've started to run the diff-based user code test suite on this PR at3f66f6c. You can monitor the buildhere. Update:The results are in! |
typescript-bot commentedOct 17, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Heya@ahejlsberg, I've started to run the abridged perf test suite on this PR at3f66f6c. You can monitor the buildhere. Update:The results are in! |
typescript-bot commentedOct 17, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Heya@ahejlsberg, I've started to run the extended test suite on this PR at3f66f6c. You can monitor the buildhere. |
Sweater-Baron commentedMar 17, 2023
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:
|
janbiasi commentedMar 20, 2023
@Sweater-Baron Yes it is, your example is working perfectly fine running version 5.0.2 (seeplayground). If you use VSCode make sure that you use your local TypeScript version and not VSCode's version. |
JordanDC2 commentedMar 21, 2023 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Is there any plan to implement enum extension like in this playground (or some other way to extend enums)?: |
isaackhoo commentedMar 21, 2023
I have set my vscode to use 5.0.2, but im still getting the error |
WORMSS commentedMar 21, 2023
@JordanDC2 Not 100% exactly what you asked for.. |
JordanDC2 commentedMar 22, 2023
@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 |
Seriosus commentedMar 27, 2023
Man, you saved me, thank you. |
tka85 commentedApr 1, 2023 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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 theplayground 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 |
Regiden commentedApr 28, 2023
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? |
gabriel-calin-lazar commentedMay 5, 2023
how come would be really useful to not have this not so obvious difference you can check some exampleshere |
@gabriel-calin-lazar See my commenthere. |
vtarelkin commentedMay 18, 2023
@tka85 any ideas about it? Also have TS above 5 and it complains |
tka85 commentedMay 19, 2023
@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. |
vtarelkin commentedMay 19, 2023
@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. |
nick4fake commentedJul 26, 2023 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@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 |
Uh oh!
There was an error while loading.Please reload this page.
Since#9407 TypeScript has had two kinds of enum types:Numeric enum types andliteral enum types (also known asunion 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 aconstant 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.