-
Notifications
You must be signed in to change notification settings - Fork 13.4k
For consistency, don't make inferences for unused type parameters when comparing two references to the same generic type. #26847
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
Closed
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| //// [independentTypeParameter.ts] | ||
| // Repro for #26815 | ||
| interface Foo<T> {} | ||
| function f<T>(arg: Foo<T>): T { return undefined!; } | ||
| let x: Foo<number> = {}; | ||
| f(x); | ||
|
|
||
|
|
||
| //// [independentTypeParameter.js] | ||
| function f(arg) { return undefined; } | ||
| var x = {}; | ||
| f(x); |
23 changes: 23 additions & 0 deletions
23
tests/baselines/reference/independentTypeParameter.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,23 @@ | ||
| === tests/cases/compiler/independentTypeParameter.ts === | ||
| // Repro for #26815 | ||
| interface Foo<T> {} | ||
| >Foo : Symbol(Foo, Decl(independentTypeParameter.ts, 0, 0)) | ||
| >T : Symbol(T, Decl(independentTypeParameter.ts, 1, 14)) | ||
|
|
||
| function f<T>(arg: Foo<T>): T { return undefined!; } | ||
| >f : Symbol(f, Decl(independentTypeParameter.ts, 1, 19)) | ||
| >T : Symbol(T, Decl(independentTypeParameter.ts, 2, 11)) | ||
| >arg : Symbol(arg, Decl(independentTypeParameter.ts, 2, 14)) | ||
| >Foo : Symbol(Foo, Decl(independentTypeParameter.ts, 0, 0)) | ||
| >T : Symbol(T, Decl(independentTypeParameter.ts, 2, 11)) | ||
| >T : Symbol(T, Decl(independentTypeParameter.ts, 2, 11)) | ||
| >undefined : Symbol(undefined) | ||
|
|
||
| let x: Foo<number> = {}; | ||
| >x : Symbol(x, Decl(independentTypeParameter.ts, 3, 3)) | ||
| >Foo : Symbol(Foo, Decl(independentTypeParameter.ts, 0, 0)) | ||
|
|
||
| f(x); | ||
| >f : Symbol(f, Decl(independentTypeParameter.ts, 1, 19)) | ||
| >x : Symbol(x, Decl(independentTypeParameter.ts, 3, 3)) | ||
|
|
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,18 @@ | ||
| === tests/cases/compiler/independentTypeParameter.ts === | ||
| // Repro for #26815 | ||
| interface Foo<T> {} | ||
| function f<T>(arg: Foo<T>): T { return undefined!; } | ||
| >f : <T>(arg: Foo<T>) => T | ||
| >arg : Foo<T> | ||
| >undefined! : undefined | ||
| >undefined : undefined | ||
|
|
||
| let x: Foo<number> = {}; | ||
| >x : Foo<number> | ||
| >{} : {} | ||
|
|
||
| f(x); | ||
| >f(x) : {} | ||
| >f : <T>(arg: Foo<T>) => T | ||
| >x : Foo<number> | ||
|
|
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,5 @@ | ||
| // Repro for #26815 | ||
| interface Foo<T> {} | ||
| function f<T>(arg: Foo<T>): T { return undefined!; } | ||
| let x: Foo<number> = {}; | ||
| f(x); |
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
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.
This is setting off breaking change alarm bells for me. What's the impact here?
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.
If a user (like this test case) only relies on inference to match up different parameterizations of the same generic interface
Collection<T, U>and never passes a subinterface or an implementing class where aCollection<T, U>is expected, then they may have gotten away (until this PR) with having no reference toTorUinCollection<T, U>. After this PR, no inference will be made and their code will likely fail to type check until dummy properties referencingTandUare added, as I've done to the test case. If a user expects inference to work from a subinterface or an implementing class toCollection<T, U>, then they would already have seen the problem and would already have been forced to add the dummy properties.Does that answer your question? Perhaps running the PR against your real-world code suite is the best way to get an idea of who's relying on the current inconsistent and undocumented behavior.
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.
So we'll start effectively requiring the use of structures similar to phantom data. At some point do we make an unreferenced type parameter as a proper error?
Uh oh!
There was an error while loading. Please reload this page.
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.
As stated in the FAQ, we already require this in all cases except the one inconsistent case I'm fixing in this PR.
Careful, in
interface Foo<T> { x?: Foo<T>; }, the type parameter is referenced but TypeScript's variance check correctly determines that it's nevertheless independent. The error should be for an independent type parameter.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.
This pattern is extremely widely-(ab)used. I don't think we can move forward with this PR under this limitation.
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.
This case is the only case that the PR changes, so if you don't want to change this case, I guess I'll abandon the PR. I wish you had told me here before I did the work.