Prefer covariant inferences with deeper type argument nesting#4389
Prefer covariant inferences with deeper type argument nesting#4389ahejlsberg wants to merge 5 commits into
Conversation
|
This PR supersedes #3391, which isn't quite the right way to solve the issue. |
There was a problem hiding this comment.
Pull request overview
This PR adjusts the checker’s generic type inference to prefer covariant inference candidates derived from more deeply-nested type arguments, addressing #1789’s failure to infer T correctly for unions like T[] | T[][].
Changes:
- Track type-argument nesting depth during inference and use it to prioritize covariant inference candidates.
- Add a new compiler regression test (
nestedGenericTypeInference.ts) covering the reported array-depth union scenario (and a few related nesting shapes). - Update compiler baselines and submodule baselines impacted by the inference behavior change.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
internal/checker/inference.go |
Tracks inference depth during inferFromTypeArguments and uses it to order/replace covariant inference candidates. |
internal/checker/checker.go |
Extends InferenceInfo to record candidate depths alongside covariant candidates. |
testdata/tests/cases/compiler/nestedGenericTypeInference.ts |
New regression test for nested/union generic inference depth behavior (Fixes #1789). |
testdata/baselines/reference/compiler/nestedGenericTypeInference.types |
New types baseline for the added regression test. |
testdata/baselines/reference/compiler/nestedGenericTypeInference.symbols |
New symbols baseline for the added regression test. |
testdata/baselines/reference/submodule/conformance/assignmentCompatWithGenericCallSignatures2.errors.txt |
Updated submodule baseline reflecting changed diagnostics. |
testdata/baselines/reference/submodule/conformance/assignmentCompatWithGenericCallSignatures2.errors.txt.diff |
Newly-added submodule diff capturing a new divergence vs upstream diagnostics. |
| assignmentCompatWithGenericCallSignatures2.ts(15,1): error TS2322: Type 'B' is not assignable to type 'A'. | ||
| - Types of parameters 'y' and 'y' are incompatible. | ||
| - Type 'T[]' is not assignable to type 'T'. | ||
| - 'T' could be instantiated with an arbitrary type which could be unrelated to 'T[]'. | ||
| + Types of parameters 'x' and 'x' are incompatible. |
|
@typescript-bot test it |
|
@ahejlsberg Here they are:
tscComparison Report - baseline..pr
System info unknown
Hosts
Scenarios
Developer Information: |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
|
Not understanding why typescript-bot is refusing to run the top400 tests. |
|
@typescript-bot test top400 |
1 similar comment
|
@typescript-bot test top400 |
|
@jakebailey Here are the results of running the top 400 repos with tsc comparing Everything looks good! |
|
@typescript-bot test top1000 |
|
@DanielRosenwasser Here are the results of running the top 400 repos with tsc comparing Everything looks good! |
With this PR we give preference to covariant inferences made from type arguments with deeper nesting. Intuitively, a successful inference from a type argument with deeper nesting is of higher quality because we've stripped away more layers of type instantiations that otherwise might skew the results. Specifically, when inferring from
Array<string> | Array<Array<string>>toArray<T> | Array<Array<T>>, the inference ofstringwe make from relatingArray<Array<string>>toArray<Array<T>>is of higher quality than the inference ofArray<string>we make relatingArray<Array<string>>toArray<T>.The PR only tracks type argument inference depths for covariant inferences. We could potentially also track for contravariant inferences, though they're rare enough that it doesn't seem worth it.
Fixes #1789.