Skip to content

Discard types that reduce to never before discriminating by discriminable items #62275

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 1 commit into
base: main
Choose a base branch
from

Conversation

Andarist
Copy link
Contributor

fixes #62256

@Copilot Copilot AI review requested due to automatic review settings August 11, 2025 22:20
@github-project-automation github-project-automation bot moved this to Not started in PR Backlog Aug 11, 2025
@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Aug 11, 2025
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes an issue where TypeScript's type discrimination was not properly handling union types that reduce to never. The fix ensures that types reducing to never are discarded before performing discrimination by discriminable items.

  • Adds a compiler test case demonstrating the fix for contextual typing by discriminable unions
  • Updates the type checker to filter out types that reduce to never during discrimination
  • Includes baseline test files showing the expected type behavior

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
tests/cases/compiler/contextuallyTypedByDiscriminableUnion2.ts New test case demonstrating the fix for discriminable union typing with never-reducing types
tests/baselines/reference/contextuallyTypedByDiscriminableUnion2.types Expected type output baseline for the new test case
tests/baselines/reference/contextuallyTypedByDiscriminableUnion2.symbols Expected symbol output baseline for the new test case
src/compiler/checker.ts Core fix that excludes never-reducing types during discrimination

interface EnableA {
readonly enableA: true;
// this introduces a conflicting property with some of the other members of MyComponentProps
// making relevant final union members reduced nevers
Copy link
Preview

Copilot AI Aug 11, 2025

Choose a reason for hiding this comment

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

The comment should be more specific about which members have conflicting properties and how this leads to never types. Consider explaining the specific conflict between EnableA requiring enableB: true and DisableB requiring enableB: false.

Suggested change
// making relevant final union members reduced nevers
// This introduces a conflicting property: EnableA requires enableB: true,
// but DisableB requires enableB: false. As a result, any union member combining
// EnableA and DisableB is reduced to never, since enableB cannot be both true and false.

Copilot uses AI. Check for mistakes.

interface EnableA {
readonly enableA: true;
// this introduces a conflicting property with some of the other members of MyComponentProps
// making relevant final union members reduced nevers
Copy link
Preview

Copilot AI Aug 11, 2025

Choose a reason for hiding this comment

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

This comment contains unclear language. 'reduced nevers' should be 'reduce to never' for grammatical correctness and clarity.

Suggested change
// making relevant final union members reduced nevers
// making relevant final union members reduce to never

Copilot uses AI. Check for mistakes.

@jakebailey
Copy link
Member

@typescript-bot test it
@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Aug 12, 2025

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

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

@jakebailey
Copy link
Member

For clarity, this is an effect of #61828

@typescript-bot
Copy link
Collaborator

typescript-bot commented Aug 12, 2025

Hey @jakebailey, I've packed this into an installable tgz. You can install it for testing by referencing it in your package.json like so:

{
    "devDependencies": {
        "typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/165861/artifacts?artifactName=tgz&fileId=355D6E9E3DA1674AADE9A5F08A00BA56F5D84B5A5D5EC83FEB4C572185313D8102&fileName=/typescript-6.0.0-insiders.20250812.tgz"
    }
}

and then running npm install.


There is also a playground for this build and an npm module you can use via "typescript": "npm:@typescript-deploys/[email protected]".;

@typescript-bot
Copy link
Collaborator

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

Everything looks the same!

You can check the log here.

@typescript-bot
Copy link
Collaborator

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

There were infrastructure failures potentially unrelated to your change:

  • 1 instance of "Git clone failed"

Otherwise...

Everything looks good!

@typescript-bot
Copy link
Collaborator

@jakebailey
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,370 62,370 ~ ~ ~ p=1.000 n=6
Types 50,386 50,386 ~ ~ ~ p=1.000 n=6
Memory used 194,783k (± 0.97%) 194,797k (± 1.05%) ~ 192,931k 196,732k p=0.810 n=6
Parse Time 1.30s (± 1.29%) 1.31s (± 1.04%) ~ 1.29s 1.32s p=0.437 n=6
Bind Time 0.73s (± 0.56%) 0.73s ~ ~ ~ p=0.405 n=6
Check Time 9.76s (± 0.30%) 9.77s (± 0.45%) ~ 9.73s 9.85s p=0.936 n=6
Emit Time 2.74s (± 0.67%) 2.73s (± 0.54%) ~ 2.71s 2.75s p=0.625 n=6
Total Time 14.52s (± 0.34%) 14.53s (± 0.25%) ~ 14.49s 14.60s p=0.627 n=6
angular-1 - node (v18.15.0, x64)
Errors 56 56 ~ ~ ~ p=1.000 n=6
Symbols 948,914 948,914 ~ ~ ~ p=1.000 n=6
Types 410,884 410,884 ~ ~ ~ p=1.000 n=6
Memory used 1,226,371k (± 0.00%) 1,226,365k (± 0.00%) ~ 1,226,330k 1,226,415k p=0.936 n=6
Parse Time 6.51s (± 1.19%) 6.50s (± 0.57%) ~ 6.47s 6.55s p=0.871 n=6
Bind Time 1.88s (± 0.29%) 1.87s (± 0.71%) ~ 1.85s 1.89s p=0.663 n=6
Check Time 32.06s (± 0.35%) 32.02s (± 0.21%) ~ 31.94s 32.13s p=0.688 n=6
Emit Time 14.78s (± 0.38%) 14.77s (± 0.86%) ~ 14.61s 14.89s p=0.936 n=6
Total Time 55.23s (± 0.31%) 55.16s (± 0.35%) ~ 54.92s 55.44s p=0.298 n=6
mui-docs - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 2,545,693 2,545,693 ~ ~ ~ p=1.000 n=6
Types 902,474 902,469 -5 (- 0.00%) ~ ~ p=0.001 n=6
Memory used 2,829,889k (± 0.01%) 2,829,836k (± 0.01%) ~ 2,829,582k 2,830,061k p=0.471 n=6
Parse Time 8.74s (± 0.33%) 8.74s (± 0.29%) ~ 8.69s 8.76s p=0.557 n=6
Bind Time 2.24s (± 0.28%) 2.23s (± 1.09%) ~ 2.19s 2.25s p=0.345 n=6
Check Time 85.84s (± 0.49%) 86.08s (± 0.51%) ~ 85.72s 86.89s p=0.261 n=6
Emit Time 0.59s (±120.15%) 1.27s (±83.19%) ~ 0.30s 2.24s p=0.170 n=6
Total Time 97.42s (± 0.58%) 98.31s (± 0.94%) ~ 97.01s 99.29s p=0.093 n=6
self-build-src - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 1,227,066 1,227,066 ~ ~ ~ p=1.000 n=6
Types 267,480 267,480 ~ ~ ~ p=1.000 n=6
Memory used 2,424,080k (± 6.15%) 2,363,217k (± 0.01%) ~ 2,362,811k 2,363,703k p=0.689 n=6
Parse Time 5.25s (± 0.95%) 5.19s (± 0.88%) ~ 5.12s 5.24s p=0.109 n=6
Bind Time 1.78s (± 1.16%) 1.80s (± 1.52%) ~ 1.77s 1.84s p=0.368 n=6
Check Time 35.38s (± 0.55%) 35.35s (± 0.16%) ~ 35.30s 35.46s p=0.149 n=6
Emit Time 3.04s (± 4.02%) 2.98s (± 0.55%) ~ 2.96s 3.00s p=0.376 n=6
Total Time 45.45s (± 0.58%) 45.34s (± 0.21%) ~ 45.20s 45.49s 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,227,066 1,227,066 ~ ~ ~ p=1.000 n=6
Types 267,480 267,480 ~ ~ ~ p=1.000 n=6
Memory used 2,797,935k (±14.27%) 3,162,649k (± 0.03%) ~ 3,161,570k 3,163,834k p=0.230 n=6
Parse Time 8.46s (± 1.45%) 8.56s (± 0.61%) ~ 8.47s 8.63s p=0.261 n=6
Bind Time 2.69s (± 0.96%) 2.62s (± 1.68%) -0.06s (- 2.36%) 2.55s 2.67s p=0.016 n=6
Check Time 53.02s (± 0.39%) 53.26s (± 0.41%) ~ 52.93s 53.54s p=0.128 n=6
Emit Time 4.44s (± 2.37%) 4.34s (± 2.11%) ~ 4.19s 4.44s p=0.093 n=6
Total Time 68.62s (± 0.43%) 68.77s (± 0.46%) ~ 68.28s 69.06s p=0.230 n=6
self-compiler - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 262,555 262,555 ~ ~ ~ p=1.000 n=6
Types 107,165 107,165 ~ ~ ~ p=1.000 n=6
Memory used 441,962k (± 0.01%) 441,954k (± 0.01%) ~ 441,875k 442,003k p=0.936 n=6
Parse Time 3.54s (± 0.71%) 3.53s (± 1.09%) ~ 3.48s 3.57s p=0.806 n=6
Bind Time 1.32s (± 1.04%) 1.32s (± 0.88%) ~ 1.31s 1.34s p=0.737 n=6
Check Time 18.97s (± 0.23%) 18.91s (± 0.37%) ~ 18.83s 19.01s p=0.093 n=6
Emit Time 1.52s (± 1.07%) 1.53s (± 0.53%) ~ 1.52s 1.54s p=0.186 n=6
Total Time 25.37s (± 0.12%) 25.30s (± 0.23%) -0.07s (- 0.30%) 25.19s 25.34s p=0.012 n=6
ts-pre-modules - node (v18.15.0, x64)
Errors 71 71 ~ ~ ~ p=1.000 n=6
Symbols 225,367 225,367 ~ ~ ~ p=1.000 n=6
Types 94,290 94,290 ~ ~ ~ p=1.000 n=6
Memory used 371,128k (± 0.02%) 371,118k (± 0.01%) ~ 371,028k 371,197k p=0.748 n=6
Parse Time 2.88s (± 1.29%) 2.87s (± 1.67%) ~ 2.81s 2.93s p=0.570 n=6
Bind Time 1.60s (± 0.51%) 1.58s (± 0.87%) -0.02s (- 1.25%) 1.56s 1.59s p=0.015 n=6
Check Time 16.44s (± 0.31%) 16.40s (± 0.27%) ~ 16.32s 16.45s p=0.226 n=6
Emit Time 0.00s 0.00s ~ ~ ~ p=1.000 n=6
Total Time 20.92s (± 0.40%) 20.84s (± 0.28%) ~ 20.74s 20.90s p=0.196 n=6
vscode - node (v18.15.0, x64)
Errors 1 1 ~ ~ ~ p=1.000 n=6
Symbols 3,848,833 3,848,833 ~ ~ ~ p=1.000 n=6
Types 1,213,113 1,213,113 ~ ~ ~ p=1.000 n=6
Memory used 3,681,893k (± 0.01%) 3,682,043k (± 0.02%) ~ 3,681,610k 3,683,242k p=0.810 n=6
Parse Time 15.36s (± 0.60%) 15.30s (± 0.65%) ~ 15.21s 15.44s p=0.298 n=6
Bind Time 5.06s (± 2.99%) 5.05s (± 3.22%) ~ 4.89s 5.21s p=0.748 n=6
Check Time 102.68s (± 2.65%) 101.14s (± 2.35%) ~ 99.30s 105.78s p=0.298 n=6
Emit Time 31.96s (± 8.51%) 39.22s (±23.54%) ~ 30.71s 48.11s p=0.298 n=6
Total Time 155.06s (± 1.49%) 160.72s (± 5.32%) ~ 151.07s 169.78s p=0.575 n=6
webpack - node (v18.15.0, x64)
Errors 0 0 ~ ~ ~ p=1.000 n=6
Symbols 320,441 320,441 ~ ~ ~ p=1.000 n=6
Types 139,219 139,219 ~ ~ ~ p=1.000 n=6
Memory used 476,928k (± 0.03%) 476,943k (± 0.02%) ~ 476,820k 477,018k p=0.810 n=6
Parse Time 4.32s (± 0.62%) 4.35s (± 0.41%) +0.03s (+ 0.77%) 4.32s 4.37s p=0.029 n=6
Bind Time 1.83s (± 1.02%) 1.84s (± 1.11%) ~ 1.82s 1.87s p=0.293 n=6
Check Time 21.11s (± 0.37%) 21.15s (± 0.50%) ~ 21.00s 21.30s p=0.520 n=6
Emit Time 0.00s (±154.76%) 0.00s (±244.70%) ~ 0.00s 0.01s p=0.595 n=6
Total Time 27.25s (± 0.27%) 27.34s (± 0.45%) ~ 27.19s 27.51s p=0.229 n=6
xstate-main - node (v18.15.0, x64)
Errors 30 30 ~ ~ ~ p=1.000 n=6
Symbols 663,768 663,768 ~ ~ ~ p=1.000 n=6
Types 198,416 198,416 ~ ~ ~ p=1.000 n=6
Memory used 570,560k (± 0.03%) 570,728k (± 0.02%) ~ 570,553k 570,868k p=0.128 n=6
Parse Time 4.28s (± 0.65%) 4.30s (± 0.54%) ~ 4.27s 4.32s p=0.418 n=6
Bind Time 1.33s (± 0.78%) 1.32s (± 1.58%) ~ 1.29s 1.34s p=0.120 n=6
Check Time 20.24s (± 2.05%) 19.98s (± 1.72%) ~ 19.75s 20.68s p=0.377 n=6
Emit Time 0.00s 0.01s (±109.43%) ~ 0.00s 0.01s p=0.071 n=6
Total Time 25.86s (± 1.55%) 25.60s (± 1.46%) ~ 25.33s 26.35s p=0.471 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

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

Everything looks good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

Typescript 5.9 can no longer derive this type, becomes 'any' instead
3 participants