Skip to content

Conversation

ahejlsberg
Copy link
Member

Implements what I describe here.

Fixes #1727.

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 fixes a translation error that disabled union discrimination optimization. The issue was caused by incorrect condition checking and an extra closing brace that broke the control flow in the type mapping logic.

  • Consolidated discriminant validation logic in mapTypesByKeyProperty
  • Fixed condition inversion in getMatchingUnionConstituentForObjectLiteral
  • Removed extraneous closing brace that was breaking function structure

Reviewed Changes

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

File Description
internal/checker/relater.go Consolidated discriminant nil and literal type checks, removed extra closing brace
internal/checker/checker.go Fixed inverted condition for key property name validation
testdata/tests/cases/compiler/missingDiscriminants2.ts Added test case for discriminated unions with >10 cases to verify the optimization works
testdata/baselines/reference/compiler/missingDiscriminants2.* Generated baseline files for the new test case

ahejlsberg added a commit to microsoft/TypeScript that referenced this pull request Sep 28, 2025
}
if !duplicate {
count++
if discriminant == nil || !isLiteralType(discriminant) {
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't this behave differently from the original code? The original code would still continue the loop of the discriminate was undefined.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, but the exit is intended. We only want to build a map when all constituents have a discriminant. When some constituents lack a discriminant, we want to skip the map optimization and go through the regular discriminateTypeByDiscriminableItems code path.

jakebailey
jakebailey previously approved these changes Sep 28, 2025
@jakebailey
Copy link
Member

Hm, microsoft/TypeScript#62501 (comment) shows a break in DT

@jakebailey jakebailey dismissed their stale review September 28, 2025 21:24

Meant to mention the breaks

@ahejlsberg ahejlsberg added this pull request to the merge queue Sep 29, 2025
ahejlsberg added a commit to microsoft/TypeScript that referenced this pull request Sep 29, 2025
Merged via the queue into main with commit 9f6a04c Sep 29, 2025
22 checks passed
@ahejlsberg ahejlsberg deleted the fix-1727 branch September 29, 2025 21:51
saralhemnani

This comment was marked as duplicate.

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.

tsc vs tsgo: inference mismatch of parameter in passed callback
3 participants