Fix discriminated union inference ordering issue with flatMap - #63948
Draft
Ryan Cavanaugh (RyanCavanaugh) with Copilot wants to merge 2 commits into
Draft
Fix discriminated union inference ordering issue with flatMap#63948Ryan Cavanaugh (RyanCavanaugh) with Copilot wants to merge 2 commits into
Ryan Cavanaugh (RyanCavanaugh) with Copilot wants to merge 2 commits into
Conversation
Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix inconsistency involving discriminated union and flatMap
Fix discriminated union inference ordering issue with flatMap
Aug 21, 2026
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.
getSingleCommonSupertypepicks the wrong inference candidate when neither of two candidates is a strict supertype of the other, because union constituent ordering differs between the Go and JS compilers — causing them to break ties inconsistently and produce diverging results for the same code (e.g. a discriminated union callback passed toArray.prototype.flatMap).Root cause
A prior fix attempt (backported in #63328 from typescript-go#3301) addressed this by replacing the subtype-based tiebreak in
getSingleCommonSupertypewith a blanket assignability check. This resolved the reported case but regressed real-world code (an Inquirer.js deep-merge/theme-typing pattern), since asymmetric assignability isn't a sound universal tiebreaker — it can favor an overly loose candidate whenever candidates differ only in property optionality.Digging further, the actual root cause is narrower:
propertiesRelatedTo'srequireOptionalPropertiescheck is relaxed only for object literal types, but candidate literal types lose their "object literal" flag once widened during inference candidate collection — incorrectly subjecting them to the stricter rule meant only for non-literal types.Fix
getSingleCommonSupertype's tiebreak untouched (still subtype-based), avoiding the previously-observed regression.ObjectFlagsWidenedObjectLiteral, retained on types produced bygetWidenedTypeOfObjectLiteralwhen their pre-widening origin was an object literal.requireOptionalPropertiesinpropertiesRelatedToto also treat such widened types leniently, matching the treatment they'd receive before widening.Added a compiler test (
discriminatedUnionFlatMap.ts) covering the reported scenario.