-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Experiment: Remove lower priority from contextual signature instantiation return type inference #58321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
andrewbranch
wants to merge
2
commits into
microsoft:main
from
andrewbranch:experiment/contextual-instantiation-priority
Closed
Experiment: Remove lower priority from contextual signature instantiation return type inference #58321
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
45 changes: 45 additions & 0 deletions
45
tests/baselines/reference/callbackAssignabilityErrorMessage.errors.txt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
callbackAssignabilityErrorMessage.ts(31,1): error TS2322: Type 'Source1' is not assignable to type 'Target'. | ||
Types of property 'map' are incompatible. | ||
Types of parameters 'callbackfn' and 'callbackfn' are incompatible. | ||
Type 'T | undefined' is not assignable to type 'T'. | ||
'T' could be instantiated with an arbitrary type which could be unrelated to 'T | undefined'. | ||
|
||
|
||
==== callbackAssignabilityErrorMessage.ts (1 errors) ==== | ||
export interface Source1 { | ||
map: <S>(callbackfn: (value: string) => S) => S[]; | ||
} | ||
|
||
export interface Target { | ||
map: <T>(callbackfn: (value: string) => T | undefined) => T[]; | ||
} | ||
|
||
declare let s1: Source1; | ||
declare let t: Target; | ||
|
||
// During the following assignment, `Source1["map"]` gets | ||
// instantiated with the contextual type `Target["map"]` before checking | ||
// assignability. To do that, an inference is made to `S`. For `Source1`, | ||
// the only candidate is `T | undefined` from the return type of `callbackfn`. | ||
// Inference also runs on `map` return types `S[]` and `T[]`, but the result | ||
// is discarded because it’s run with a lower inference priority. As a result, | ||
// we end up seeing if the contextually instantiated source signature: | ||
// | ||
// (callbackfn: (value: string) => T | undefined) => (T | undefined)[] | ||
// | ||
// is assignable to the target signature: | ||
// | ||
// (callbackfn: (value: string) => T | undefined) => T[] | ||
// | ||
// and the return types cause the failed assignability. But as a human reader | ||
// interpreting why `s1` is not assignable to `t`, I instead equate `S` and `T` | ||
// and identify the “real” problem as the return type of `callbackfn` in `Target` | ||
// (`T | undefined`) being a supertype of the one in `Source1` (`S` → `T`). | ||
|
||
t = s1; // Bad: instantiates `S` with `T | undefined`, fails `map` return type assignability | ||
~ | ||
!!! error TS2322: Type 'Source1' is not assignable to type 'Target'. | ||
!!! error TS2322: Types of property 'map' are incompatible. | ||
!!! error TS2322: Types of parameters 'callbackfn' and 'callbackfn' are incompatible. | ||
!!! error TS2322: Type 'T | undefined' is not assignable to type 'T'. | ||
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'T | undefined'. |
56 changes: 56 additions & 0 deletions
56
tests/baselines/reference/callbackAssignabilityErrorMessage.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//// [tests/cases/compiler/callbackAssignabilityErrorMessage.ts] //// | ||
|
||
//// [callbackAssignabilityErrorMessage.ts] | ||
export interface Source1 { | ||
map: <S>(callbackfn: (value: string) => S) => S[]; | ||
} | ||
|
||
export interface Target { | ||
map: <T>(callbackfn: (value: string) => T | undefined) => T[]; | ||
} | ||
|
||
declare let s1: Source1; | ||
declare let t: Target; | ||
|
||
// During the following assignment, `Source1["map"]` gets | ||
// instantiated with the contextual type `Target["map"]` before checking | ||
// assignability. To do that, an inference is made to `S`. For `Source1`, | ||
// the only candidate is `T | undefined` from the return type of `callbackfn`. | ||
// Inference also runs on `map` return types `S[]` and `T[]`, but the result | ||
// is discarded because it’s run with a lower inference priority. As a result, | ||
// we end up seeing if the contextually instantiated source signature: | ||
// | ||
// (callbackfn: (value: string) => T | undefined) => (T | undefined)[] | ||
// | ||
// is assignable to the target signature: | ||
// | ||
// (callbackfn: (value: string) => T | undefined) => T[] | ||
// | ||
// and the return types cause the failed assignability. But as a human reader | ||
// interpreting why `s1` is not assignable to `t`, I instead equate `S` and `T` | ||
// and identify the “real” problem as the return type of `callbackfn` in `Target` | ||
// (`T | undefined`) being a supertype of the one in `Source1` (`S` → `T`). | ||
|
||
t = s1; // Bad: instantiates `S` with `T | undefined`, fails `map` return type assignability | ||
|
||
//// [callbackAssignabilityErrorMessage.js] | ||
// During the following assignment, `Source1["map"]` gets | ||
// instantiated with the contextual type `Target["map"]` before checking | ||
// assignability. To do that, an inference is made to `S`. For `Source1`, | ||
// the only candidate is `T | undefined` from the return type of `callbackfn`. | ||
// Inference also runs on `map` return types `S[]` and `T[]`, but the result | ||
// is discarded because it’s run with a lower inference priority. As a result, | ||
// we end up seeing if the contextually instantiated source signature: | ||
// | ||
// (callbackfn: (value: string) => T | undefined) => (T | undefined)[] | ||
// | ||
// is assignable to the target signature: | ||
// | ||
// (callbackfn: (value: string) => T | undefined) => T[] | ||
// | ||
// and the return types cause the failed assignability. But as a human reader | ||
// interpreting why `s1` is not assignable to `t`, I instead equate `S` and `T` | ||
// and identify the “real” problem as the return type of `callbackfn` in `Target` | ||
// (`T | undefined`) being a supertype of the one in `Source1` (`S` → `T`). | ||
t = s1; // Bad: instantiates `S` with `T | undefined`, fails `map` return type assignability | ||
export {}; |
58 changes: 58 additions & 0 deletions
58
tests/baselines/reference/callbackAssignabilityErrorMessage.symbols
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
//// [tests/cases/compiler/callbackAssignabilityErrorMessage.ts] //// | ||
|
||
=== callbackAssignabilityErrorMessage.ts === | ||
export interface Source1 { | ||
>Source1 : Symbol(Source1, Decl(callbackAssignabilityErrorMessage.ts, 0, 0)) | ||
|
||
map: <S>(callbackfn: (value: string) => S) => S[]; | ||
>map : Symbol(Source1.map, Decl(callbackAssignabilityErrorMessage.ts, 0, 26)) | ||
>S : Symbol(S, Decl(callbackAssignabilityErrorMessage.ts, 1, 8)) | ||
>callbackfn : Symbol(callbackfn, Decl(callbackAssignabilityErrorMessage.ts, 1, 11)) | ||
>value : Symbol(value, Decl(callbackAssignabilityErrorMessage.ts, 1, 24)) | ||
>S : Symbol(S, Decl(callbackAssignabilityErrorMessage.ts, 1, 8)) | ||
>S : Symbol(S, Decl(callbackAssignabilityErrorMessage.ts, 1, 8)) | ||
} | ||
|
||
export interface Target { | ||
>Target : Symbol(Target, Decl(callbackAssignabilityErrorMessage.ts, 2, 1)) | ||
|
||
map: <T>(callbackfn: (value: string) => T | undefined) => T[]; | ||
>map : Symbol(Target.map, Decl(callbackAssignabilityErrorMessage.ts, 4, 25)) | ||
>T : Symbol(T, Decl(callbackAssignabilityErrorMessage.ts, 5, 8)) | ||
>callbackfn : Symbol(callbackfn, Decl(callbackAssignabilityErrorMessage.ts, 5, 11)) | ||
>value : Symbol(value, Decl(callbackAssignabilityErrorMessage.ts, 5, 24)) | ||
>T : Symbol(T, Decl(callbackAssignabilityErrorMessage.ts, 5, 8)) | ||
>T : Symbol(T, Decl(callbackAssignabilityErrorMessage.ts, 5, 8)) | ||
} | ||
|
||
declare let s1: Source1; | ||
>s1 : Symbol(s1, Decl(callbackAssignabilityErrorMessage.ts, 8, 11)) | ||
>Source1 : Symbol(Source1, Decl(callbackAssignabilityErrorMessage.ts, 0, 0)) | ||
|
||
declare let t: Target; | ||
>t : Symbol(t, Decl(callbackAssignabilityErrorMessage.ts, 9, 11)) | ||
>Target : Symbol(Target, Decl(callbackAssignabilityErrorMessage.ts, 2, 1)) | ||
|
||
// During the following assignment, `Source1["map"]` gets | ||
// instantiated with the contextual type `Target["map"]` before checking | ||
// assignability. To do that, an inference is made to `S`. For `Source1`, | ||
// the only candidate is `T | undefined` from the return type of `callbackfn`. | ||
// Inference also runs on `map` return types `S[]` and `T[]`, but the result | ||
// is discarded because it’s run with a lower inference priority. As a result, | ||
// we end up seeing if the contextually instantiated source signature: | ||
// | ||
// (callbackfn: (value: string) => T | undefined) => (T | undefined)[] | ||
// | ||
// is assignable to the target signature: | ||
// | ||
// (callbackfn: (value: string) => T | undefined) => T[] | ||
// | ||
// and the return types cause the failed assignability. But as a human reader | ||
// interpreting why `s1` is not assignable to `t`, I instead equate `S` and `T` | ||
// and identify the “real” problem as the return type of `callbackfn` in `Target` | ||
// (`T | undefined`) being a supertype of the one in `Source1` (`S` → `T`). | ||
|
||
t = s1; // Bad: instantiates `S` with `T | undefined`, fails `map` return type assignability | ||
>t : Symbol(t, Decl(callbackAssignabilityErrorMessage.ts, 9, 11)) | ||
>s1 : Symbol(s1, Decl(callbackAssignabilityErrorMessage.ts, 8, 11)) | ||
|
58 changes: 58 additions & 0 deletions
58
tests/baselines/reference/callbackAssignabilityErrorMessage.types
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
//// [tests/cases/compiler/callbackAssignabilityErrorMessage.ts] //// | ||
|
||
=== callbackAssignabilityErrorMessage.ts === | ||
export interface Source1 { | ||
map: <S>(callbackfn: (value: string) => S) => S[]; | ||
>map : <S>(callbackfn: (value: string) => S) => S[] | ||
> : ^ ^^ ^^ ^^^^^ | ||
>callbackfn : (value: string) => S | ||
> : ^ ^^ ^^^^^ | ||
>value : string | ||
> : ^^^^^^ | ||
} | ||
|
||
export interface Target { | ||
map: <T>(callbackfn: (value: string) => T | undefined) => T[]; | ||
>map : <T>(callbackfn: (value: string) => T | undefined) => T[] | ||
> : ^ ^^ ^^ ^^^^^ | ||
>callbackfn : (value: string) => T | undefined | ||
> : ^ ^^ ^^^^^ | ||
>value : string | ||
> : ^^^^^^ | ||
} | ||
|
||
declare let s1: Source1; | ||
>s1 : Source1 | ||
> : ^^^^^^^ | ||
|
||
declare let t: Target; | ||
>t : Target | ||
> : ^^^^^^ | ||
|
||
// During the following assignment, `Source1["map"]` gets | ||
// instantiated with the contextual type `Target["map"]` before checking | ||
// assignability. To do that, an inference is made to `S`. For `Source1`, | ||
// the only candidate is `T | undefined` from the return type of `callbackfn`. | ||
// Inference also runs on `map` return types `S[]` and `T[]`, but the result | ||
// is discarded because it’s run with a lower inference priority. As a result, | ||
// we end up seeing if the contextually instantiated source signature: | ||
// | ||
// (callbackfn: (value: string) => T | undefined) => (T | undefined)[] | ||
// | ||
// is assignable to the target signature: | ||
// | ||
// (callbackfn: (value: string) => T | undefined) => T[] | ||
// | ||
// and the return types cause the failed assignability. But as a human reader | ||
// interpreting why `s1` is not assignable to `t`, I instead equate `S` and `T` | ||
// and identify the “real” problem as the return type of `callbackfn` in `Target` | ||
// (`T | undefined`) being a supertype of the one in `Source1` (`S` → `T`). | ||
|
||
t = s1; // Bad: instantiates `S` with `T | undefined`, fails `map` return type assignability | ||
>t = s1 : Source1 | ||
> : ^^^^^^^ | ||
>t : Target | ||
> : ^^^^^^ | ||
>s1 : Source1 | ||
> : ^^^^^^^ | ||
|
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wrote this test to exercise a very specific inference code path in the PR that introduced it, so it's not grounded in a real world scenario, but I don't think the new error is worse? Maybe the connection between the error and the actual code is more surprising now just because we end up inferring
T = { [x: string]: number }
instead ofT = U
.Maybe that's a separate problem with signature comparison: it's not immediately clear to users that we've done inference in order to compare two signatures, and it's not immediately clear what the result of that inference was.