Fix inference from unions of arrays with different nesting depths#3391
Open
Yiin wants to merge 2 commits intomicrosoft:mainfrom
Open
Fix inference from unions of arrays with different nesting depths#3391Yiin wants to merge 2 commits intomicrosoft:mainfrom
Yiin wants to merge 2 commits intomicrosoft:mainfrom
Conversation
When inferring T from a parameter typed T[] | T[][], the closely-matched inference pass cross-matches all Array constituents regardless of nesting depth. Because CompareTypes sorts union constituents by type flags (Object < Union), source types like Value[][] sort before Value[] when Value is a union or non-reference object type. The first cross-match (Value[][] to T[]) produces the wrong candidate T = Value[], and findLeftmostType picks it. Add an intermediate "deeply matched" pass between the existing isTypeOrBaseIdenticalTo and isTypeCloselyMatchedBy passes. This pass matches reference types only when their type arguments have compatible nesting structure — if a source arg is a nested reference to the same outer type (e.g., Array<Array<...>> inside Array), the target arg must also be nested, and vice versa. Same-depth pairs are inferred first, preventing the incorrect cross-match. Fixes microsoft#1789, fixes microsoft#3370.
Author
|
@microsoft-github-policy-service agree |
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.
Fixes #1789, fixes #3370.
When inferring
Tfrom a parameter typedT[] | T[][], the closely-matched inference pass cross-matches allArrayconstituents regardless of nesting depth.CompareTypessorts union constituents by type flags (Object<Union), so source types likeValue[][]sort beforeValue[]whenValueis a union or non-reference object type. The first cross-match (Value[][]toT[]) produces the wrong candidateT = Value[], andfindLeftmostTypepicks it.This PR adds an intermediate "deeply matched" pass between the existing
isTypeOrBaseIdenticalToandisTypeCloselyMatchedBypasses. The new pass matches reference types only when their type arguments have compatible nesting structure — if a source arg is a nested reference to the same outer type (e.g.,Array<Array<...>>insideArray), the target arg must also be nested, and vice versa. Same-depth pairs are inferred first, preventing the incorrect cross-match.Notes
stableTypeOrderingis enabled (the default since it was added in Feb 2026), but no upstream test covers theT[] | T[][]pattern with non-primitive type arguments.inferFromTypescalls don't increase.