Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12298,12 +12298,9 @@ namespace ts {
// a digest of the type comparisons that occur for each type argument when instantiations of the
// generic type are structurally compared. We infer the variance information by comparing
// instantiations of the generic type for type arguments with known relations. The function
// returns the emptyArray singleton if we're not in strictFunctionTypes mode or if the function
// has been invoked recursively for the given generic type.
// returns the emptyArray singleton if the function has been invoked recursively for the given
// generic type.
function getVariances(type: GenericType): Variance[] {
if (!strictFunctionTypes) {
return emptyArray;
}
const typeParameters = type.typeParameters || emptyArray;
let variances = type.variances;
if (!variances) {
Expand Down Expand Up @@ -13379,7 +13376,7 @@ namespace ts {
if (i < variances.length && variances[i] === Variance.Contravariant) {
inferFromContravariantTypes(sourceTypes[i], targetTypes[i]);
}
else {
else if (!(i < variances.length && variances[i] === Variance.Independent)) {
inferFromTypes(sourceTypes[i], targetTypes[i]);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@ declare module Immutable {
>Seq : typeof Seq

function isSeq(maybeSeq: any): maybeSeq is Seq.Indexed<any> | Seq.Keyed<any, any>;
>isSeq : (maybeSeq: any) => maybeSeq is Keyed<any, any> | Indexed<any>
>isSeq : (maybeSeq: any) => maybeSeq is Indexed<any> | Keyed<any, any>
>maybeSeq : any
>Seq : any
>Seq : any
Expand Down
12 changes: 12 additions & 0 deletions tests/baselines/reference/independentTypeParameter.js
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 tests/baselines/reference/independentTypeParameter.symbols
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))

18 changes: 18 additions & 0 deletions tests/baselines/reference/independentTypeParameter.types
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>

5 changes: 5 additions & 0 deletions tests/cases/compiler/independentTypeParameter.ts
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);
2 changes: 2 additions & 0 deletions tests/cases/fourslash/genericCombinators3.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/// <reference path='fourslash.ts'/>

////interface Collection<T, U> {
//// dummyT: T;
Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Contributor Author

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 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.

Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Contributor Author

@mattmccutchen mattmccutchen Sep 18, 2018

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.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

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.

//// dummyU: U;
////}
////
////interface Combinators {
Expand Down
1 change: 1 addition & 0 deletions tests/cases/fourslash/staticGenericOverloads1.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// <reference path='fourslash.ts'/>

////class A<T> {
//// dummy: T;
//// static B<S>(v: A<S>): A<S>;
//// static B<S>(v: S): A<S>;
//// static B<S>(v: any): A<S> {
Expand Down