Skip to content

Improve type discrimination algorithm #1085

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

Merged
merged 3 commits into from
Jun 9, 2025
Merged

Improve type discrimination algorithm #1085

merged 3 commits into from
Jun 9, 2025

Conversation

ahejlsberg
Copy link
Member

Implements what I suggest here.

We might consider back-porting this to the old code base.

Fixes #1020.

Copy link
Contributor

@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 enhances the type discrimination algorithm in the checker, adds new tests for missing discriminants, and updates baselines for error outputs.

  • Added missingDiscriminants.ts tests to cover cases where discriminants are partially missing.
  • Updated reference baselines under submodule/compiler and compiler to reflect the revised discrimination behavior.
  • Modified relater.go to change how TernaryMaybe is applied within the discrimination loop.

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.

Show a summary per file
File Description
testdata/tests/cases/compiler/missingDiscriminants.ts New test cases for missing discriminants behavior
testdata/baselines/reference/submodule/compiler/discriminateWithMissingProperty.errors.txt.diff Cleared outdated submodule error baseline
testdata/baselines/reference/submodule/compiler/discriminateWithMissingProperty.errors.txt Removed old submodule error baseline content
testdata/baselines/reference/compiler/missingDiscriminants.types New type-check baseline for missing discriminants tests
testdata/baselines/reference/compiler/missingDiscriminants.symbols New symbol baseline for missing discriminants tests
testdata/baselines/reference/compiler/missingDiscriminants.errors.txt New error baseline for missing discriminants tests
internal/checker/relater.go Adjusted discrimination logic to separate property-check and match
Comments suppressed due to low confidence (1)

internal/checker/relater.go:1228

  • The inserted closing brace here appears to end the for loop prematurely, changing the intended nesting and control flow. Remove or reposition it so that the loop and subsequent logic maintain their original scopes.
}

@jakebailey
Copy link
Member

Ported it over in microsoft/TypeScript#61828 and it fails the self test. I wonder why this doesn't fail in CI here.

@jakebailey
Copy link
Member

Ah, because the failure isn't in src/compiler, which is what we test here given we don't have build mode.

@ahejlsberg ahejlsberg added this pull request to the merge queue Jun 9, 2025
Merged via the queue into main with commit 7e04e77 Jun 9, 2025
23 checks passed
@ahejlsberg ahejlsberg deleted the fix-1020 branch June 9, 2025 23:20
@Eigilak
Copy link

Eigilak commented Jun 10, 2025

The man, the myth, the legend 🫡

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Discrepancies in behavior of spread of union type
3 participants