Fix type inference ordering inconsistency with discriminated unions and flatMap#3301
Draft
Fix type inference ordering inconsistency with discriminated unions and flatMap#3301
Conversation
…ty as tiebreaker in getSingleCommonSupertype Agent-Logs-Url: https://github.com/microsoft/typescript-go/sessions/47e26ee2-385e-4444-a9ef-5e5b6ba3ed27 Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix inconsistency involving discriminated union and flatMap
Fix type inference ordering inconsistency with discriminated unions and flatMap
Mar 30, 2026
Member
|
This caused one break: microsoft/TypeScript#63328 (comment) |
|
@RyanCavanaugh Here are the results of running the user tests with tsc comparing There were infrastructure failures potentially unrelated to your change:
Otherwise... Everything looks good! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When inferring a common supertype from candidates, Go and TypeScript can encounter candidates in different orders due to union constituent ordering differences. This caused
flatMapcallbacks returning discriminated union branches to fail in Go but succeed in TypeScript.Root Cause
getSingleCommonSupertypeusedisTypeSubtypeOfas the fallback tiebreaker. The subtype relation setsrequireOptionalProperties = truefor non-fresh object types, so a widened{ op: "remove" }(which lostObjectFlagsObjectLiteralaftergetWidenedTypeOfObjectLiteral) would fail the subtype check againstInputOp = { op: "add" } | { op: "remove"; value?: Array<unknown> }— despite being clearly assignable to it. With reversed candidate ordering, Go picked the wrong type.Fix
Replace the
isTypeSubtypeOffallback with asymmetric assignability (isTypeAssignableTo(s, t) && !isTypeAssignableTo(t, s)):{ op: "remove" }→InputOp) correctly promote to the more general type regardless of ordering.{ x: number; y?: number }and{ x: number; z?: number }) are symmetric, so selection remains order-based — preserving existing behavior.This follows the approach suggested by @ahejlsberg in the issue.