For consistency, don't make inferences for unused type parameters when comparing two references to the same generic type.#26847
Conversation
comparing two references to the same generic type. We want this even when strictFunctionTypes is off. Variance computation was previously disabled when strictFunctionTypes is off, but I removed that check without breaking any tests. Fixes microsoft#26815.
e77ad1a to
b5fc594
Compare
| /// <reference path='fourslash.ts'/> | ||
|
|
||
| ////interface Collection<T, U> { | ||
| //// dummyT: T; |
There was a problem hiding this comment.
This is setting off breaking change alarm bells for me. What's the impact here?
There was a problem hiding this comment.
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 a Collection<T, U> is expected, then they may have gotten away (until this PR) with having no reference to T or U in Collection<T, U>. After this PR, no inference will be made and their code will likely fail to type check until dummy properties referencing T and U are added, as I've done to the test case. If a user expects inference to work from a subinterface or an implementing class to Collection<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.
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?
There was a problem hiding this comment.
So we'll start effectively requiring the use of structures similar to phantom data.
As stated in the FAQ, we already require this in all cases except the one inconsistent case I'm fixing in this PR.
At some point do we make an unreferenced type parameter as a proper error?
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.
then they may have gotten away (until this PR) with having no reference to T or U in Collection<T, U>
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.
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.
We want this even when strictFunctionTypes is off. Variance computation
was previously disabled when strictFunctionTypes is off, but I removed
that check without breaking any tests.
Fixes #26815.