-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Make contravariant inferences only from pure contravariant positions #27357
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
//// [bivariantInferences.ts] | ||
// Repro from #27337 | ||
|
||
interface Array<T> { | ||
equalsShallow<T>(this: ReadonlyArray<T>, other: ReadonlyArray<T>): boolean; | ||
} | ||
|
||
declare const a: (string | number)[] | null[] | undefined[] | {}[]; | ||
declare const b: (string | number)[] | null[] | undefined[] | {}[]; | ||
|
||
let x = a.equalsShallow(b); | ||
|
||
|
||
//// [bivariantInferences.js] | ||
"use strict"; | ||
// Repro from #27337 | ||
var x = a.equalsShallow(b); |
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,31 @@ | ||
=== tests/cases/conformance/types/typeRelationships/typeInference/bivariantInferences.ts === | ||
// Repro from #27337 | ||
|
||
interface Array<T> { | ||
>Array : Symbol(Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(bivariantInferences.ts, 0, 0)) | ||
>T : Symbol(T, Decl(lib.es5.d.ts, --, --), Decl(bivariantInferences.ts, 2, 16)) | ||
|
||
equalsShallow<T>(this: ReadonlyArray<T>, other: ReadonlyArray<T>): boolean; | ||
>equalsShallow : Symbol(Array.equalsShallow, Decl(bivariantInferences.ts, 2, 20)) | ||
>T : Symbol(T, Decl(bivariantInferences.ts, 3, 18)) | ||
>this : Symbol(this, Decl(bivariantInferences.ts, 3, 21)) | ||
>ReadonlyArray : Symbol(ReadonlyArray, Decl(lib.es5.d.ts, --, --)) | ||
>T : Symbol(T, Decl(bivariantInferences.ts, 3, 18)) | ||
>other : Symbol(other, Decl(bivariantInferences.ts, 3, 44)) | ||
>ReadonlyArray : Symbol(ReadonlyArray, Decl(lib.es5.d.ts, --, --)) | ||
>T : Symbol(T, Decl(bivariantInferences.ts, 3, 18)) | ||
} | ||
|
||
declare const a: (string | number)[] | null[] | undefined[] | {}[]; | ||
>a : Symbol(a, Decl(bivariantInferences.ts, 6, 13)) | ||
|
||
declare const b: (string | number)[] | null[] | undefined[] | {}[]; | ||
>b : Symbol(b, Decl(bivariantInferences.ts, 7, 13)) | ||
|
||
let x = a.equalsShallow(b); | ||
>x : Symbol(x, Decl(bivariantInferences.ts, 9, 3)) | ||
>a.equalsShallow : Symbol(equalsShallow, Decl(bivariantInferences.ts, 2, 20), Decl(bivariantInferences.ts, 2, 20), Decl(bivariantInferences.ts, 2, 20), Decl(bivariantInferences.ts, 2, 20)) | ||
>a : Symbol(a, Decl(bivariantInferences.ts, 6, 13)) | ||
>equalsShallow : Symbol(equalsShallow, Decl(bivariantInferences.ts, 2, 20), Decl(bivariantInferences.ts, 2, 20), Decl(bivariantInferences.ts, 2, 20), Decl(bivariantInferences.ts, 2, 20)) | ||
>b : Symbol(b, Decl(bivariantInferences.ts, 7, 13)) | ||
|
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,26 @@ | ||
=== tests/cases/conformance/types/typeRelationships/typeInference/bivariantInferences.ts === | ||
// Repro from #27337 | ||
|
||
interface Array<T> { | ||
equalsShallow<T>(this: ReadonlyArray<T>, other: ReadonlyArray<T>): boolean; | ||
>equalsShallow : <T>(this: ReadonlyArray<T>, other: ReadonlyArray<T>) => boolean | ||
>this : ReadonlyArray<T> | ||
>other : ReadonlyArray<T> | ||
} | ||
|
||
declare const a: (string | number)[] | null[] | undefined[] | {}[]; | ||
>a : (string | number)[] | null[] | undefined[] | {}[] | ||
>null : null | ||
|
||
declare const b: (string | number)[] | null[] | undefined[] | {}[]; | ||
>b : (string | number)[] | null[] | undefined[] | {}[] | ||
>null : null | ||
|
||
let x = a.equalsShallow(b); | ||
>x : boolean | ||
>a.equalsShallow(b) : boolean | ||
>a.equalsShallow : (<T>(this: ReadonlyArray<T>, other: ReadonlyArray<T>) => boolean) | (<T>(this: ReadonlyArray<T>, other: ReadonlyArray<T>) => boolean) | (<T>(this: ReadonlyArray<T>, other: ReadonlyArray<T>) => boolean) | (<T>(this: ReadonlyArray<T>, other: ReadonlyArray<T>) => boolean) | ||
>a : (string | number)[] | null[] | undefined[] | {}[] | ||
>equalsShallow : (<T>(this: ReadonlyArray<T>, other: ReadonlyArray<T>) => boolean) | (<T>(this: ReadonlyArray<T>, other: ReadonlyArray<T>) => boolean) | (<T>(this: ReadonlyArray<T>, other: ReadonlyArray<T>) => boolean) | (<T>(this: ReadonlyArray<T>, other: ReadonlyArray<T>) => boolean) | ||
>b : (string | number)[] | null[] | undefined[] | {}[] | ||
|
12 changes: 12 additions & 0 deletions
12
tests/cases/conformance/types/typeRelationships/typeInference/bivariantInferences.ts
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,12 @@ | ||
// @strict: true | ||
|
||
// Repro from #27337 | ||
|
||
interface Array<T> { | ||
equalsShallow<T>(this: ReadonlyArray<T>, other: ReadonlyArray<T>): boolean; | ||
} | ||
|
||
declare const a: (string | number)[] | null[] | undefined[] | {}[]; | ||
declare const b: (string | number)[] | null[] | undefined[] | {}[]; | ||
|
||
let x = a.equalsShallow(b); |
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.
Doesn't this only need to be set on the specifically contravariant comparison within
forEachMatchingParameterType
? Also, could we not use a singlevariance
variable for bothbivariant
andcontravariant
context tracking (seeing as we have a variance enum already)?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.
The
contravariant
flag continues to track whether the position is co- or contra-variant, regardless of whether we've descended into abivariant
position. Doing it this way, using two separate boolean variables, makes the logic that flips the flag easy. I looked at keeping it in a single variable but didn't like the complexity that came with it.