Skip to content
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

Use comparability for discriminant properties when narrowing types for a default switch clause #61211

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Andarist
Copy link
Contributor

fixes #61207

@@ -29113,7 +29113,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (!hasDefaultClause) {
return caseType;
}
const defaultType = filterType(type, t => !(isUnitLikeType(t) && contains(switchTypes, t.flags & TypeFlags.Undefined ? undefinedType : getRegularTypeOfLiteralType(extractUnitType(t)))));
const defaultType = filterType(type, t => !(isUnitLikeType(t) && contains(switchTypes, t.flags & TypeFlags.Undefined ? undefinedType : getRegularTypeOfLiteralType(extractUnitType(t)), (t1, t2) => isUnitType(t1) && areTypesComparable(t1, t2))));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This essentially matches the logic used by narrowTypeByEquality here

            if (isUnitType(valueType)) {
                return filterType(type, t => !(isUnitLikeType(t) && areTypesComparable(t, valueType)));
            }

As we can see, the valueType has to pass isUnitType there and the equivalent of the valueType here are all the types in switchTypes. Note that in the case of switch/case here that check mainly ensures we ignore neverType as that's comparable to everything. neverType is specifically used within switchTypes to indicate that this switch statement has a default case (see getTypeOfSwitchClause here and hasDefaultClause here)

@DanielRosenwasser
Copy link
Member

@typescript-bot test it

@typescript-bot
Copy link
Collaborator

typescript-bot commented Feb 18, 2025

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

@typescript-bot
Copy link
Collaborator

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

Everything looks good!

@typescript-bot
Copy link
Collaborator

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

Everything looks the same!

You can check the log here.

@typescript-bot
Copy link
Collaborator

@DanielRosenwasser
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 34 34 ~ ~ ~ p=1.000 n=6
Symbols 62,390 62,390 ~ ~ ~ p=1.000 n=6
Types 50,395 50,395 ~ ~ ~ p=1.000 n=6
Memory used 193,006k (± 0.07%) 194,721k (± 1.01%) ~ 192,917k 196,553k p=0.936 n=6
Parse Time 1.31s (± 0.84%) 1.31s (± 1.04%) ~ 1.28s 1.32s p=0.929 n=6
Bind Time 0.73s 0.73s ~ ~ ~ p=1.000 n=6
Check Time 9.72s (± 0.33%) 9.85s (± 0.25%) +0.13s (+ 1.29%) 9.81s 9.87s p=0.005 n=6
Emit Time 2.72s (± 0.72%) 2.73s (± 0.71%) ~ 2.71s 2.76s p=0.167 n=6
Total Time 14.48s (± 0.39%) 14.61s (± 0.13%) +0.14s (+ 0.94%) 14.59s 14.64s p=0.005 n=6
angular-1 - node (v18.15.0, x64)
Errors 37 37 ~ ~ ~ p=1.000 n=6
Symbols 948,488 948,488 ~ ~ ~ p=1.000 n=6
Types 411,006 411,006 ~ ~ ~ p=1.000 n=6
Memory used 1,224,196k (± 0.00%) 1,224,238k (± 0.01%) ~ 1,224,172k 1,224,335k p=0.336 n=6
Parse Time 6.66s (± 0.63%) 6.62s (± 0.78%) ~ 6.57s 6.68s p=0.220 n=6
Bind Time 1.89s (± 0.27%) 1.88s (± 0.64%) ~ 1.87s 1.90s p=0.142 n=6
Check Time 31.89s (± 0.26%) 31.73s (± 0.15%) -0.16s (- 0.50%) 31.66s 31.79s p=0.005 n=6
Emit Time 15.22s (± 0.27%) 15.24s (± 0.56%) ~ 15.14s 15.35s p=1.000 n=6
Total Time 55.67s (± 0.12%) 55.48s (± 0.23%) -0.19s (- 0.34%) 55.25s 55.61s p=0.010 n=6
mui-docs - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 2,347,799 2,347,799 ~ ~ ~ p=1.000 n=6
Types 845,614 845,614 ~ ~ ~ p=1.000 n=6
Memory used 2,120,232k (± 0.01%) 2,120,126k (± 0.01%) ~ 2,119,964k 2,120,268k p=0.173 n=6
Parse Time 8.79s (± 0.53%) 8.75s (± 0.37%) ~ 8.72s 8.81s p=0.170 n=6
Bind Time 2.91s (± 0.73%) 2.93s (± 0.50%) ~ 2.91s 2.95s p=0.075 n=6
Check Time 86.46s (± 0.39%) 86.37s (± 0.37%) ~ 85.95s 86.73s p=0.936 n=6
Emit Time 0.20s (± 5.11%) 0.19s (± 3.92%) 🟩-0.01s (- 6.50%) 0.18s 0.20s p=0.046 n=6
Total Time 98.36s (± 0.36%) 98.25s (± 0.32%) ~ 97.84s 98.55s p=0.936 n=6
self-build-src - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 1,228,510 1,228,515 +5 (+ 0.00%) ~ ~ p=0.001 n=6
Types 266,960 266,961 +1 (+ 0.00%) ~ ~ p=0.001 n=6
Memory used 2,358,701k (± 0.02%) 2,358,352k (± 0.03%) ~ 2,357,678k 2,359,123k p=0.378 n=6
Parse Time 5.20s (± 1.18%) 5.19s (± 1.20%) ~ 5.14s 5.28s p=0.810 n=6
Bind Time 1.78s (± 0.90%) 1.79s (± 1.79%) ~ 1.76s 1.84s p=0.935 n=6
Check Time 35.26s (± 0.21%) 35.33s (± 0.34%) ~ 35.16s 35.51s p=0.261 n=6
Emit Time 2.96s (± 2.04%) 3.06s (± 4.91%) ~ 2.94s 3.36s p=0.149 n=6
Total Time 45.19s (± 0.17%) 45.36s (± 0.43%) ~ 45.16s 45.65s p=0.173 n=6
self-build-src-public-api - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 1,228,510 1,228,515 +5 (+ 0.00%) ~ ~ p=0.001 n=6
Types 266,960 266,961 +1 (+ 0.00%) ~ ~ p=0.001 n=6
Memory used 2,912,881k (±12.88%) 2,575,965k (±11.24%) ~ 2,426,612k 3,153,816k p=0.230 n=6
Parse Time 6.91s (± 1.65%) 6.83s (± 1.15%) ~ 6.75s 6.96s p=0.230 n=6
Bind Time 2.16s (± 1.87%) 2.15s (± 0.81%) ~ 2.12s 2.17s p=0.936 n=6
Check Time 42.76s (± 0.55%) 42.76s (± 0.27%) ~ 42.62s 42.90s p=0.936 n=6
Emit Time 3.52s (± 1.82%) 3.49s (± 2.44%) ~ 3.36s 3.61s p=0.687 n=6
Total Time 55.33s (± 0.54%) 55.23s (± 0.23%) ~ 55.02s 55.41s p=0.128 n=6
self-compiler - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 262,816 262,821 +5 (+ 0.00%) ~ ~ p=0.001 n=6
Types 106,833 106,834 +1 (+ 0.00%) ~ ~ p=0.001 n=6
Memory used 440,544k (± 0.01%) 440,495k (± 0.02%) ~ 440,341k 440,570k p=0.471 n=6
Parse Time 3.56s (± 0.60%) 3.54s (± 0.86%) ~ 3.49s 3.57s p=0.375 n=6
Bind Time 1.33s (± 1.30%) 1.32s (± 0.64%) ~ 1.30s 1.32s p=0.122 n=6
Check Time 18.94s (± 0.11%) 19.01s (± 0.31%) +0.07s (+ 0.40%) 18.95s 19.12s p=0.016 n=6
Emit Time 1.52s (± 1.13%) 1.53s (± 0.99%) ~ 1.51s 1.55s p=0.677 n=6
Total Time 25.34s (± 0.14%) 25.40s (± 0.16%) ~ 25.35s 25.45s p=0.089 n=6
ts-pre-modules - node (v18.15.0, x64)
Errors 70 70 ~ ~ ~ p=1.000 n=6
Symbols 226,113 226,113 ~ ~ ~ p=1.000 n=6
Types 94,488 94,488 ~ ~ ~ p=1.000 n=6
Memory used 371,289k (± 0.01%) 371,293k (± 0.01%) ~ 371,230k 371,317k p=0.936 n=6
Parse Time 2.32s (± 0.74%) 2.33s (± 0.79%) ~ 2.30s 2.35s p=0.366 n=6
Bind Time 1.34s (± 1.68%) 1.34s (± 1.68%) ~ 1.32s 1.37s p=1.000 n=6
Check Time 13.70s (± 0.20%) 13.71s (± 0.25%) ~ 13.69s 13.78s p=0.871 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 17.36s (± 0.32%) 17.38s (± 0.12%) ~ 17.36s 17.42s p=0.574 n=6
vscode - node (v18.15.0, x64)
Errors 3 3 ~ ~ ~ p=1.000 n=6
Symbols 3,221,220 3,221,220 ~ ~ ~ p=1.000 n=6
Types 1,092,664 1,092,666 +2 (+ 0.00%) ~ ~ p=0.001 n=6
Memory used 3,293,569k (± 0.00%) 3,293,391k (± 0.01%) -178k (- 0.01%) 3,293,070k 3,293,553k p=0.031 n=6
Parse Time 14.29s (± 0.48%) 14.24s (± 0.55%) ~ 14.17s 14.35s p=0.229 n=6
Bind Time 4.62s (± 0.61%) 4.64s (± 1.82%) ~ 4.56s 4.79s p=1.000 n=6
Check Time 90.11s (± 2.70%) 88.67s (± 1.28%) ~ 87.79s 90.90s p=0.471 n=6
Emit Time 25.97s (± 9.18%) 25.72s (± 8.49%) ~ 22.88s 27.65s p=0.688 n=6
Total Time 135.00s (± 3.18%) 133.27s (± 2.15%) ~ 129.52s 137.15s p=0.471 n=6
webpack - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 293,866 293,866 ~ ~ ~ p=1.000 n=6
Types 119,617 119,617 ~ ~ ~ p=1.000 n=6
Memory used 447,165k (± 0.02%) 447,200k (± 0.03%) ~ 447,058k 447,424k p=0.936 n=6
Parse Time 4.11s (± 1.35%) 4.09s (± 1.03%) ~ 4.05s 4.15s p=0.520 n=6
Bind Time 1.77s (± 0.94%) 1.76s (± 0.66%) ~ 1.75s 1.78s p=0.287 n=6
Check Time 18.74s (± 0.83%) 18.75s (± 0.51%) ~ 18.63s 18.89s p=0.872 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 24.62s (± 0.75%) 24.60s (± 0.53%) ~ 24.46s 24.81s p=0.575 n=6
xstate-main - node (v18.15.0, x64)
Errors 5 5 ~ ~ ~ p=1.000 n=6
Symbols 558,084 558,084 ~ ~ ~ p=1.000 n=6
Types 186,415 186,415 ~ ~ ~ p=1.000 n=6
Memory used 495,046k (± 0.01%) 494,961k (± 0.03%) ~ 494,721k 495,062k p=0.378 n=6
Parse Time 3.40s (± 0.70%) 3.40s (± 0.85%) ~ 3.37s 3.45s p=0.872 n=6
Bind Time 1.18s (± 1.46%) 1.19s (± 0.92%) ~ 1.18s 1.21s p=0.731 n=6
Check Time 19.57s (± 0.43%) 19.82s (± 2.42%) ~ 19.49s 20.63s p=1.000 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 24.16s (± 0.34%) 24.41s (± 1.96%) ~ 24.06s 25.22s p=0.936 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

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

Everything looks good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Not started
3 participants