Fix inference from unions of arrays with different nesting depths#3391
Open
Yiin wants to merge 6 commits into
Open
Fix inference from unions of arrays with different nesting depths#3391Yiin wants to merge 6 commits into
Yiin wants to merge 6 commits into
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 |
# Conflicts: # internal/checker/inference.go
Member
|
@typescript-bot test it |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a TypeScript-go type inference bug when inferring T from union parameters like T[] | T[][], where union constituent ordering could cause a mismatched array-depth cross-match and infer T one level off.
Changes:
- Adds a new “deep match” inference pass between identical-match and close-match passes to prefer same-nesting matches.
- Introduces a regression compiler test covering union, object, and primitive cases for
T[] | T[][]inference. - Adds new reference baselines for the regression test (
.types,.symbols,.js).
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| internal/checker/inference.go | Adds a new deep-matching pass and a helper predicate to avoid cross-matching arrays with different nesting depths. |
| testdata/tests/cases/compiler/inferenceUnionArrayDepth.ts | New regression test exercising the problematic `T[] |
| testdata/baselines/reference/compiler/inferenceUnionArrayDepth.types | Baseline capturing expected inferred types for the new regression test. |
| testdata/baselines/reference/compiler/inferenceUnionArrayDepth.symbols | Baseline capturing expected symbol info for the new regression test. |
| testdata/baselines/reference/compiler/inferenceUnionArrayDepth.js | Baseline capturing expected emit output for the new regression test. |
Comment on lines
+1184
to
+1201
| if s.objectFlags&ObjectFlagsReference != 0 && t.objectFlags&ObjectFlagsReference != 0 { | ||
| sr := s.AsTypeReference() | ||
| tr := t.AsTypeReference() | ||
| sArgs := sr.resolvedTypeArguments | ||
| tArgs := tr.resolvedTypeArguments | ||
| if len(sArgs) == len(tArgs) { | ||
| for i := range sArgs { | ||
| // Check that type arguments have matching nesting structure: if the source arg | ||
| // is a nested reference to the same outer type (e.g., Array<Array<...>>), the | ||
| // target arg must also be nested, and vice versa. | ||
| saIsNestedRef := sArgs[i].objectFlags&ObjectFlagsReference != 0 && sArgs[i].AsTypeReference().target == sr.target | ||
| taIsNestedRef := tArgs[i].objectFlags&ObjectFlagsReference != 0 && tArgs[i].AsTypeReference().target == tr.target | ||
| if saIsNestedRef != taIsNestedRef { | ||
| return false | ||
| } | ||
| } | ||
| } | ||
| } |
Comment on lines
+111
to
+114
| // Next, infer between deeply matching source and target constituents and remove the | ||
| // matching types. Types deeply match when they are instantiations of the same object | ||
| // type AND their type arguments are themselves closely matched. This prevents incorrect | ||
| // cross-matching of types like Array<T> with Array<T[]> in unions like T[] | T[][]. |
|
@jakebailey Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@jakebailey Here are the results of running the top 400 repos with tsc comparing Everything looks good! |
Comment on lines
+1184
to
+1204
| if s.objectFlags&ObjectFlagsReference != 0 && t.objectFlags&ObjectFlagsReference != 0 { | ||
| sr := s.AsTypeReference() | ||
| tr := t.AsTypeReference() | ||
| sArgs := c.getTypeArguments(s) | ||
| tArgs := c.getTypeArguments(t) | ||
| if len(sArgs) != len(tArgs) { | ||
| return false | ||
| } | ||
| for i := range sArgs { | ||
| // Check that type arguments have matching nesting structure: if the source arg | ||
| // is a nested reference to the same outer type (e.g., Array<Array<...>>), the | ||
| // target arg must also be nested, and vice versa. | ||
| saIsNestedRef := sArgs[i].objectFlags&ObjectFlagsReference != 0 && sArgs[i].AsTypeReference().target == sr.target | ||
| taIsNestedRef := tArgs[i].objectFlags&ObjectFlagsReference != 0 && tArgs[i].AsTypeReference().target == tr.target | ||
| if saIsNestedRef != taIsNestedRef { | ||
| return false | ||
| } | ||
| } | ||
| } | ||
| return true | ||
| } |
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.