Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
ahejlsberg merged 14 commits intomainfromenumImprovements
Nov 3, 2022
Merged

Enum unification and improvements#50528

ahejlsberg merged 14 commits intomainfromenumImprovements
Nov 3, 2022

Conversation

ahejlsberg
Copy link
Member

@ahejlsbergahejlsberg commentedAug 29, 2022
edited
Loading

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,

enumE{A=10*10,// Numeric literal enum memberB="foo",// String literal enum memberC=bar(42)// Opaque computed enum member}

declares a typeE 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 typenumber 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

  • a number or string literal,
  • a unary+,-, or~ applied to a numeric constant expression,
  • a binary+,-,*,/,%,**,<<,>>,>>>,|,&,^ applied to two numeric constant expressions,
  • a binary+ applied to two constant expressions whereof at least one is a string,
  • a template expression where each substitution expression is a constant expression,
  • a parenthesized constant expression,
  • a dotted name (e.g.x.y.z) that references aconst variable with a constant expression initializer and no type annotation,
  • a dotted name that references an enum member with an enum literal type, or
  • a dotted name indexed by a string literal (e.g.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:

constBaseValue=10;constPrefix="/data";constenumValues{First=BaseValue,// 10Second,// 11Third// 12}constenumRoutes{Parts=`${prefix}/parts`,// "/data/parts"Invoices=`${prefix}/invoices`// "/data/invoices"}

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.

jackmcpickle, Vishtar, std4453, alexrock, jakebailey, RyanGrieb, t7yang, swese44, kirillgroshkov, tonivj5, and 70 more reacted with thumbs up emojidead-claudia, lin72h, lukeramsden, kevinbarabash, kwiatek-kainos, sokcuri, Fyko, nicojs, extradosages, amelinium23, and 8 more reacted with hooray emojidartess, ppluuums-jp, cockscomb, patricio-hondagneu-simplisafe, uriannrima, Quentame, albohlabs, Sajjon, changwoolab, daniele-orlando, and 2 more reacted with heart emojiiamravisingh, ExE-Boss, 1yzz, yukinotech, Sajjon, and changwoolab reacted with rocket emojiservice-paradis, lukeramsden, amitbeck, natalia-lafever, purlingstream, 1yzz, unao, Sajjon, and changwoolab reacted with eyes emoji
@typescript-bottypescript-bot added Author: Team For Uncommitted BugPR for untriaged, rejected, closed or missing bug labelsAug 29, 2022
@RyanCavanaugh
Copy link
Member

@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commentedAug 29, 2022
edited
Loading

Heya@RyanCavanaugh, I've started to run the tarball bundle task on this PR at50a952b. You can monitor the buildhere.

@ahejlsberg
Copy link
MemberAuthor

@typescript-bot test this
@typescript-bot user test this inline
@typescript-bot run dt
@typescript-bot perf test faster

@typescript-bot
Copy link
Collaborator

typescript-bot commentedAug 29, 2022
edited
Loading

Heya@ahejlsberg, I've started to run the extended test suite on this PR at50a952b. You can monitor the buildhere.

@typescript-bot
Copy link
Collaborator

typescript-bot commentedAug 29, 2022
edited
Loading

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
Copy link
Collaborator

typescript-bot commentedAug 29, 2022
edited
Loading

Heya@ahejlsberg, I've started to run the parallelized Definitely Typed test suite on this PR at50a952b. You can monitor the buildhere.

@typescript-bot
Copy link
Collaborator

typescript-bot commentedAug 29, 2022
edited
Loading

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!

@typescript-bot
Copy link
Collaborator

@ahejlsberg
The results of the perf run you requested are in!

Here they are:

Comparison Report - main..50528

Metricmain50528DeltaBestWorst
Angular - node (v14.15.1, x64)
Memory used338,727k (± 0.00%)338,717k (± 0.01%)-9k (- 0.00%)338,668k338,757k
Parse Time2.08s (± 0.70%)2.09s (± 0.93%)+0.02s (+ 0.82%)2.06s2.15s
Bind Time0.80s (± 0.62%)0.80s (± 0.75%)+0.00s (+ 0.00%)0.79s0.81s
Check Time5.82s (± 0.58%)5.83s (± 0.27%)+0.01s (+ 0.14%)5.79s5.87s
Emit Time6.18s (± 0.49%)6.22s (± 0.72%)+0.04s (+ 0.58%)6.16s6.37s
Total Time14.87s (± 0.27%)14.93s (± 0.40%)+0.06s (+ 0.41%)14.85s15.11s
Compiler-Unions - node (v14.15.1, x64)
Memory used192,654k (± 0.02%)192,983k (± 0.37%)+329k (+ 0.17%)192,631k195,896k
Parse Time0.86s (± 0.52%)0.85s (± 0.87%)-0.01s (- 0.58%)0.84s0.87s
Bind Time0.49s (± 0.75%)0.49s (± 0.82%)+0.00s (+ 0.62%)0.48s0.50s
Check Time6.70s (± 0.55%)6.73s (± 0.69%)+0.03s (+ 0.43%)6.64s6.85s
Emit Time2.41s (± 1.02%)2.40s (± 0.72%)-0.01s (- 0.54%)2.35s2.43s
Total Time10.46s (± 0.45%)10.47s (± 0.51%)+0.02s (+ 0.16%)10.35s10.62s
Monaco - node (v14.15.1, x64)
Memory used326,505k (± 0.01%)326,536k (± 0.00%)+31k (+ 0.01%)326,510k326,581k
Parse Time1.57s (± 0.59%)1.60s (± 1.50%)+0.03s (+ 1.65%)1.57s1.69s
Bind Time0.73s (± 0.85%)0.73s (± 0.68%)-0.00s (- 0.41%)0.72s0.74s
Check Time5.73s (± 0.51%)5.78s (± 0.51%)+0.05s (+ 0.86%)5.73s5.85s
Emit Time3.35s (± 0.61%)3.35s (± 0.67%)-0.00s (- 0.12%)3.30s3.40s
Total Time11.39s (± 0.42%)11.45s (± 0.44%)+0.06s (+ 0.55%)11.35s11.55s
TFS - node (v14.15.1, x64)
Memory used289,645k (± 0.00%)289,648k (± 0.01%)+3k (+ 0.00%)289,614k289,687k
Parse Time1.31s (± 0.83%)1.31s (± 0.80%)+0.01s (+ 0.38%)1.30s1.34s
Bind Time0.78s (± 2.29%)0.80s (± 0.75%)+0.01s (+ 1.40%)0.78s0.81s
Check Time5.36s (± 0.54%)5.35s (± 0.62%)-0.01s (- 0.24%)5.27s5.44s
Emit Time3.60s (± 0.77%)3.57s (± 0.76%)-0.03s (- 0.75%)3.51s3.63s
Total Time11.05s (± 0.38%)11.03s (± 0.59%)-0.03s (- 0.25%)10.87s11.20s
material-ui - node (v14.15.1, x64)
Memory used438,087k (± 0.06%)438,212k (± 0.00%)+125k (+ 0.03%)438,180k438,251k
Parse Time1.86s (± 0.59%)1.87s (± 0.59%)+0.01s (+ 0.38%)1.84s1.89s
Bind Time0.59s (± 0.62%)0.59s (± 1.10%)+0.00s (+ 0.34%)0.58s0.61s
Check Time13.06s (± 0.95%)12.92s (± 0.81%)-0.14s (- 1.06%)12.72s13.15s
Emit Time0.00s (± 0.00%)0.00s (± 0.00%)0.00s ( NaN%)0.00s0.00s
Total Time15.51s (± 0.81%)15.38s (± 0.72%)-0.13s (- 0.83%)15.16s15.62s
xstate - node (v14.15.1, x64)
Memory used547,388k (± 0.00%)547,403k (± 0.00%)+15k (+ 0.00%)547,362k547,442k
Parse Time2.60s (± 0.45%)2.62s (± 0.47%)+0.02s (+ 0.73%)2.60s2.65s
Bind Time0.97s (± 0.70%)0.98s (± 0.68%)+0.01s (+ 0.62%)0.97s0.99s
Check Time1.54s (± 0.62%)1.54s (± 0.42%)-0.00s (- 0.13%)1.53s1.55s
Emit Time0.07s (± 4.13%)0.07s (± 3.14%)-0.00s (- 1.39%)0.07s0.08s
Total Time5.20s (± 0.42%)5.22s (± 0.21%)+0.02s (+ 0.38%)5.19s5.24s
System
Machine Namets-ci-ubuntu
Platformlinux 4.4.0-210-generic
Architecturex64
Available Memory16 GB
Available Memory15 GB
CPUs4 × Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz
Hosts
  • node (v14.15.1, x64)
Scenarios
  • Angular - node (v14.15.1, x64)
  • Compiler-Unions - node (v14.15.1, x64)
  • Monaco - node (v14.15.1, x64)
  • TFS - node (v14.15.1, x64)
  • material-ui - node (v14.15.1, x64)
  • xstate - node (v14.15.1, x64)
BenchmarkNameIterations
Current5052810
Baselinemain10

Developer Information:

Download Benchmark

@typescript-bot
Copy link
Collaborator

@ahejlsberg Here are the results of running the user test suite comparingmain andrefs/pull/50528/merge:

Everything looks good!

@typescript-bot
Copy link
Collaborator

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
Copy link
Collaborator

typescript-bot commentedAug 29, 2022
edited
Loading

Hey@RyanCavanaugh, I've packed this intoan installable tgz. You can install it for testing by referencing it in yourpackage.json like so:

{    "devDependencies": {        "typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/133037/artifacts?artifactName=tgz&fileId=3F948668C1BD8758CB1C8E59919B3D6E945DCF3815094A729B98FCE15A366B7E02&fileName=/typescript-4.9.0-insiders.20220829.tgz"    }}

and then runningnpm install.


There is also a playgroundfor this build and annpm module you can use via"typescript": "npm:@typescript-deploys/pr-build@4.9.0-pr-50528-10".;

@ahejlsberg
Copy link
MemberAuthor

@typescript-bot test this
@typescript-bot user test this inline
@typescript-bot run dt
@typescript-bot perf test faster
@typescript-bot test top100

@typescript-bot
Copy link
Collaborator

typescript-bot commentedOct 17, 2022
edited
Loading

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
Copy link
Collaborator

typescript-bot commentedOct 17, 2022
edited
Loading

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
Copy link
Collaborator

typescript-bot commentedOct 17, 2022
edited
Loading

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
Copy link
Collaborator

typescript-bot commentedOct 17, 2022
edited
Loading

Heya@ahejlsberg, I've started to run the extended test suite on this PR at3f66f6c. You can monitor the buildhere.

@Sweater-Baron
Copy link

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:

const prefix = '/data';const enum Routes {  Parts = `${prefix}/parts`, // "/data/parts"  Invoices = `${prefix}/invoices`, // "/data/invoices"}

@janbiasi
Copy link

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.

@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.

tka85 reacted with thumbs down emoji

@JordanDC2
Copy link

JordanDC2 commentedMar 21, 2023
edited
Loading

Is there any plan to implement enum extension like in this playground (or some other way to extend enums)?:

Screen Shot 2023-03-21 at 9 01 53 AM

@isaackhoo
Copy link

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.

@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.

I have set my vscode to use 5.0.2, but im still getting the error
image

maxrbaldwin reacted with thumbs up emoji

@JordanDC2
Copy link

@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

@tka85
Copy link

tka85 commentedApr 1, 2023
edited
Loading

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.

export enum TABLES {    foo = `${config.schema}.foos`,    bar = `${config.schema}.bars`}

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 withnpm ls typescript; only 5.0.2 and no other version exists in the workspace.

maxrbaldwin, vtarelkin, williamrobertson13, Sweater-Baron, and nick4fake reacted with thumbs up emoji

@Regiden
Copy link

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.

enum TEST {    ONE = 1,    TWO}const foo: TEST = 0;

This now is an error. Was there any reason not to allow for a fallback here?

@gabriel-calin-lazar
Copy link

how come
we have strict enum usages when value is number, when using number literal or enum entry
but
we have strict enum usages when value is string, only when using enum entry - not when using string literal ?

would be really useful to not have this not so obvious difference

you can check some exampleshere

@ahejlsberg
Copy link
MemberAuthor

@gabriel-calin-lazar See my commenthere.

@vtarelkin
Copy link

@tka85 any ideas about it? Also have TS above 5 and it complains

@tka85
Copy link

@vtarelkin Nope. No response.

@jakebailey
Copy link
Member

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
Copy link

@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
image

nick4fake reacted with thumbs up emoji

@jakebailey
Copy link
Member

Please, please, please, file an issue and fill out the template.

yume-chan, ruialves92, and RyanCavanaugh reacted with thumbs up emoji

@nick4fake
Copy link

nick4fake commentedJul 26, 2023
edited
Loading

@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

@microsoftmicrosoft locked asresolvedand limited conversation to collaboratorsJul 26, 2023
@RyanCavanaugh
Copy link
Member

Please file a new issue outlining the problem you're having

Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.
Reviewers

@sandersnsandersnsandersn approved these changes

@RyanCavanaughRyanCavanaughRyanCavanaugh approved these changes

@andrewbranchandrewbranchAwaiting requested review from andrewbranch

@DanielRosenwasserDanielRosenwasserAwaiting requested review from DanielRosenwasser

@weswighamweswighamAwaiting requested review from weswigham

Assignees

@ahejlsbergahejlsberg

Labels
Author: TeamBreaking ChangeWould introduce errors in existing codeFor Milestone BugPRs that fix a bug with a specific milestone
Projects
Archived in project
21 participants
@ahejlsberg@RyanCavanaugh@typescript-bot@Jack-Works@jakebailey@WORMSS@andreymaslovru@nicojs@hackwaly@Harpush@Sweater-Baron@janbiasi@JordanDC2@isaackhoo@Seriosus@tka85@Regiden@gabriel-calin-lazar@vtarelkin@nick4fake@sandersn

[8]ページ先頭

©2009-2025 Movatter.jp