Skip to content

Support const assertions and unit type const variable references in constant expressions #59475

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

ahejlsberg
Copy link
Member

@ahejlsberg ahejlsberg commented Jul 30, 2024

With this PR we permit constant expressions to contain const assertions and references to const variables with unit types:

const C00 = "a";
const C01 = "b" as const;
const C02: "c" = "c";
declare const C03: "d";

const enum E0 {
    A = C00,
    B = C01,  // Was error, now ok
    C = C02,  // Was error, now ok
    D = C03,  // Was error, now ok
}

const C10 = 1;
const C11 = 2 as const;
const C12: 3 = 3;
declare const C13: 4;

const enum E1 {
    A = C10,
    B = C11,  // Was error, now ok
    C = C12,  // Was error, now ok
    D = C13,  // Was error, now ok
}

Note that a const variable with a type annotation may be referenced in a constant expression only if the type annotation is a string or numeric literal. Other forms of types, such as named types or union types, are not supported.

Fixes #59346.

@typescript-bot typescript-bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Jul 30, 2024
@ahejlsberg
Copy link
Member Author

@typescript-bot test it

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jul 30, 2024

Starting jobs; this comment will be updated as builds start and complete.

Command Status Results
test top400 ✅ Started ✅ Results
user test this ✅ Started ✅ Results
run dt ✅ Started ✅ Results
perf test this faster ✅ Started 👀 Results

@ahejlsberg ahejlsberg requested a review from RyanCavanaugh July 30, 2024 17:18
@typescript-bot
Copy link
Collaborator

Hey @ahejlsberg, the results of running the DT tests are ready.

Everything looks the same!

You can check the log here.

@typescript-bot
Copy link
Collaborator

@ahejlsberg Here are the results of running the user tests with tsc comparing main and refs/pull/59475/merge:

Everything looks good!

@typescript-bot
Copy link
Collaborator

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

Here they are:

tsc

Comparison Report - baseline..pr
Metric baseline pr Delta Best Worst p-value
Compiler-Unions - node (v18.15.0, x64)
Errors 30 30 ~ ~ ~ p=1.000 n=6
Symbols 62,153 62,153 ~ ~ ~ p=1.000 n=6
Types 50,242 50,242 ~ ~ ~ p=1.000 n=6
Memory used 192,882k (± 0.74%) 194,182k (± 0.97%) +1,301k (+ 0.67%) 192,303k 196,019k p=0.031 n=6
Parse Time 1.57s (± 0.52%) 1.57s (± 1.02%) ~ 1.54s 1.58s p=0.796 n=6
Bind Time 0.86s (± 0.95%) 0.85s (± 1.21%) ~ 0.84s 0.87s p=0.546 n=6
Check Time 11.36s (± 0.30%) 11.39s (± 0.65%) ~ 11.31s 11.50s p=0.573 n=6
Emit Time 3.26s (± 1.09%) 3.26s (± 0.93%) ~ 3.21s 3.30s p=1.000 n=6
Total Time 17.06s (± 0.15%) 17.08s (± 0.55%) ~ 16.95s 17.23s p=0.687 n=6
angular-1 - node (v18.15.0, x64)
Errors 7 7 ~ ~ ~ p=1.000 n=6
Symbols 945,532 945,532 ~ ~ ~ p=1.000 n=6
Types 409,507 409,507 ~ ~ ~ p=1.000 n=6
Memory used 1,221,116k (± 0.00%) 1,221,146k (± 0.00%) ~ 1,221,115k 1,221,202k p=0.066 n=6
Parse Time 7.86s (± 0.76%) 7.85s (± 0.63%) ~ 7.80s 7.93s p=0.686 n=6
Bind Time 2.22s (± 0.72%) 2.21s (± 0.74%) ~ 2.19s 2.24s p=0.625 n=6
Check Time 36.35s (± 0.43%) 36.36s (± 0.49%) ~ 36.22s 36.68s p=1.000 n=6
Emit Time 17.75s (± 0.62%) 17.78s (± 0.97%) ~ 17.51s 18.00s p=0.748 n=6
Total Time 64.17s (± 0.36%) 64.20s (± 0.39%) ~ 63.81s 64.43s p=1.000 n=6
mui-docs - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 2,439,121 2,439,121 ~ ~ ~ p=1.000 n=6
Types 1,004,407 1,004,407 ~ ~ ~ p=1.000 n=6
Memory used 2,410,535k (± 0.00%) 2,410,556k (± 0.00%) ~ 2,410,517k 2,410,630k p=0.471 n=6
Parse Time 9.94s (± 0.21%) 9.94s (± 0.20%) ~ 9.91s 9.96s p=0.807 n=6
Bind Time 2.49s (± 0.65%) 2.49s (± 0.22%) ~ 2.48s 2.49s p=0.228 n=6
Check Time 89.24s (± 2.12%) 89.04s (± 1.51%) ~ 88.06s 91.61s p=0.630 n=6
Emit Time 0.33s (± 2.29%) 0.32s (± 2.79%) ~ 0.31s 0.33s p=0.149 n=6
Total Time 102.00s (± 1.85%) 101.79s (± 1.32%) ~ 100.77s 104.37s p=0.689 n=6
self-build-src - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 1,227,815 1,227,818 +3 (+ 0.00%) ~ ~ p=0.001 n=6
Types 265,152 265,153 +1 (+ 0.00%) ~ ~ p=0.001 n=6
Memory used 2,464,884k (± 7.42%) 2,347,114k (± 0.03%) ~ 2,345,986k 2,347,910k p=0.936 n=6
Parse Time 5.97s (± 0.75%) 5.91s (± 0.50%) -0.05s (- 0.92%) 5.88s 5.96s p=0.031 n=6
Bind Time 2.27s (± 0.66%) 2.26s (± 1.03%) ~ 2.25s 2.31s p=0.120 n=6
Check Time 40.54s (± 0.54%) 40.61s (± 0.27%) ~ 40.43s 40.76s p=0.689 n=6
Emit Time 3.93s (± 0.84%) 3.94s (± 0.88%) ~ 3.88s 3.98s p=0.872 n=6
Total Time 52.73s (± 0.50%) 52.74s (± 0.17%) ~ 52.66s 52.89s p=0.575 n=6
self-build-src-public-api - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 1,227,815 1,227,818 +3 (+ 0.00%) ~ ~ p=0.001 n=6
Types 265,152 265,153 +1 (+ 0.00%) ~ ~ p=0.001 n=6
Memory used 2,420,353k (± 0.03%) 2,420,621k (± 0.01%) ~ 2,420,189k 2,421,053k p=0.810 n=6
Parse Time 6.20s (± 0.83%) 6.22s (± 0.67%) ~ 6.15s 6.27s p=0.520 n=6
Bind Time 2.04s (± 0.91%) 2.03s (± 1.16%) ~ 1.98s 2.04s p=0.324 n=6
Check Time 41.24s (± 0.78%) 41.19s (± 0.15%) ~ 41.11s 41.28s p=0.936 n=6
Emit Time 4.05s (± 1.53%) 4.04s (± 2.54%) ~ 3.95s 4.24s p=0.421 n=6
Total Time 53.55s (± 0.54%) 53.51s (± 0.36%) ~ 53.25s 53.82s p=0.936 n=6
self-compiler - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 255,990 255,993 +3 (+ 0.00%) ~ ~ p=0.001 n=6
Types 104,965 104,966 +1 (+ 0.00%) ~ ~ p=0.001 n=6
Memory used 427,919k (± 0.02%) 428,058k (± 0.05%) ~ 427,848k 428,334k p=0.471 n=6
Parse Time 3.35s (± 0.93%) 3.37s (± 0.68%) ~ 3.34s 3.39s p=0.222 n=6
Bind Time 1.32s (± 0.57%) 1.30s (± 1.33%) -0.02s (- 1.77%) 1.28s 1.32s p=0.036 n=6
Check Time 17.97s (± 0.27%) 18.02s (± 0.34%) ~ 17.94s 18.09s p=0.335 n=6
Emit Time 1.64s (± 1.30%) 1.66s (± 0.98%) ~ 1.64s 1.68s p=0.106 n=6
Total Time 24.29s (± 0.34%) 24.34s (± 0.25%) ~ 24.27s 24.41s p=0.336 n=6
ts-pre-modules - node (v18.15.0, x64)
Errors 35 35 ~ ~ ~ p=1.000 n=6
Symbols 224,931 224,931 ~ ~ ~ p=1.000 n=6
Types 94,146 94,146 ~ ~ ~ p=1.000 n=6
Memory used 370,075k (± 0.02%) 370,113k (± 0.05%) ~ 369,912k 370,329k p=0.936 n=6
Parse Time 3.45s (± 0.79%) 3.43s (± 0.66%) ~ 3.40s 3.45s p=0.120 n=6
Bind Time 1.92s (± 0.71%) 1.92s (± 0.51%) ~ 1.91s 1.93s p=0.611 n=6
Check Time 19.36s (± 0.30%) 19.32s (± 0.50%) ~ 19.23s 19.49s p=0.261 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 24.74s (± 0.26%) 24.67s (± 0.36%) ~ 24.57s 24.82s p=0.128 n=6
vscode - node (v18.15.0, x64)
Errors 11 11 ~ ~ ~ p=1.000 n=6
Symbols 2,989,199 2,989,199 ~ ~ ~ p=1.000 n=6
Types 1,028,388 1,028,428 +40 (+ 0.00%) ~ ~ p=0.001 n=6
Memory used 3,113,698k (± 0.00%) 3,113,665k (± 0.00%) ~ 3,113,608k 3,113,733k p=0.230 n=6
Parse Time 13.82s (± 0.50%) 13.81s (± 0.27%) ~ 13.78s 13.87s p=0.806 n=6
Bind Time 4.36s (± 2.67%) 4.32s (± 2.22%) ~ 4.26s 4.51s p=0.464 n=6
Check Time 79.40s (± 0.35%) 79.65s (± 0.50%) ~ 79.16s 80.11s p=0.378 n=6
Emit Time 20.57s (± 0.97%) 20.60s (± 0.41%) ~ 20.44s 20.68s p=0.297 n=6
Total Time 118.16s (± 0.35%) 118.39s (± 0.38%) ~ 117.69s 118.96s p=0.378 n=6
webpack - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 267,994 267,994 ~ ~ ~ p=1.000 n=6
Types 109,192 109,192 ~ ~ ~ p=1.000 n=6
Memory used 412,899k (± 0.02%) 412,944k (± 0.03%) ~ 412,849k 413,149k p=0.810 n=6
Parse Time 3.81s (± 0.59%) 3.81s (± 0.45%) ~ 3.79s 3.84s p=0.806 n=6
Bind Time 1.72s (± 0.30%) 1.71s (± 0.37%) ~ 1.70s 1.72s p=0.091 n=6
Check Time 16.87s (± 0.63%) 16.91s (± 0.56%) ~ 16.81s 17.08s p=0.574 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 22.40s (± 0.47%) 22.43s (± 0.41%) ~ 22.35s 22.60s p=0.423 n=6
xstate-main - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 535,921 535,921 ~ ~ ~ p=1.000 n=6
Types 176,733 176,733 ~ ~ ~ p=1.000 n=6
Memory used 477,642k (± 0.06%) 477,474k (± 0.09%) ~ 477,065k 477,930k p=0.378 n=6
Parse Time 2.80s (± 0.79%) 2.79s (± 0.70%) ~ 2.76s 2.81s p=0.190 n=6
Bind Time 1.04s (± 0.50%) 1.03s (± 1.13%) ~ 1.01s 1.04s p=0.523 n=6
Check Time 15.17s (± 0.25%) 15.18s (± 0.53%) ~ 15.11s 15.31s p=0.871 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 19.01s (± 0.25%) 19.00s (± 0.54%) ~ 18.89s 19.15s p=0.748 n=6
System info unknown
Hosts
  • node (v18.15.0, x64)
Scenarios
  • Compiler-Unions - node (v18.15.0, x64)
  • angular-1 - node (v18.15.0, x64)
  • mui-docs - node (v18.15.0, x64)
  • self-build-src - node (v18.15.0, x64)
  • self-build-src-public-api - node (v18.15.0, x64)
  • self-compiler - node (v18.15.0, x64)
  • ts-pre-modules - node (v18.15.0, x64)
  • vscode - node (v18.15.0, x64)
  • webpack - node (v18.15.0, x64)
  • xstate-main - node (v18.15.0, x64)
Benchmark Name Iterations
Current pr 6
Baseline baseline 6

Developer Information:

Download Benchmarks

@typescript-bot
Copy link
Collaborator

@ahejlsberg Here are the results of running the top 400 repos with tsc comparing main and refs/pull/59475/merge:

Everything looks good!

@daniele-orlando
Copy link

daniele-orlando commented Jan 24, 2025

@RyanCavanaugh Hi dear TypeScript maintainers, this MR is pending since 6 months and it is blocking the resolution of issue #59346, which had a milestone of TypeScript 5.7.0, which has been clearly missed.

Can we get it merged?

@Friend-LGA
Copy link

I also would love to see this merged

P.S.: looks like all tests have successfully passed loooong time ago... Can someone review this MR already, please?

@daniele-orlando
Copy link

daniele-orlando commented Feb 19, 2025

@RyanCavanaugh Is there any hope to see this merge request merged sooner or later? 6 months have passed and it is blocked for no explained reason.

cc: @jakebailey @andrewbranch @DanielRosenwasser

@IainSAP
Copy link

IainSAP commented Apr 2, 2025

Please approve and merge...thx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
Status: Not started
Development

Successfully merging this pull request may close these issues.

[proposal] Non widened string values should be valid enum values, like widened string values
5 participants