-
Notifications
You must be signed in to change notification settings - Fork 12.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22869 from Microsoft/fixConditionalTypeVariance
Fix conditional type variance
- Loading branch information
Showing
6 changed files
with
709 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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,90 @@ | ||
tests/cases/conformance/types/conditional/conditionalTypes2.ts(15,5): error TS2322: Type 'Covariant<A>' is not assignable to type 'Covariant<B>'. | ||
Type 'A' is not assignable to type 'B'. | ||
tests/cases/conformance/types/conditional/conditionalTypes2.ts(19,5): error TS2322: Type 'Contravariant<B>' is not assignable to type 'Contravariant<A>'. | ||
Type 'A' is not assignable to type 'B'. | ||
tests/cases/conformance/types/conditional/conditionalTypes2.ts(24,5): error TS2322: Type 'Invariant<B>' is not assignable to type 'Invariant<A>'. | ||
Types of property 'foo' are incompatible. | ||
Type 'B extends string ? keyof B : B' is not assignable to type 'A extends string ? keyof A : A'. | ||
Type 'keyof B' is not assignable to type 'keyof A'. | ||
tests/cases/conformance/types/conditional/conditionalTypes2.ts(25,5): error TS2322: Type 'Invariant<A>' is not assignable to type 'Invariant<B>'. | ||
Types of property 'foo' are incompatible. | ||
Type 'A extends string ? keyof A : A' is not assignable to type 'B extends string ? keyof B : B'. | ||
Type 'A' is not assignable to type 'B'. | ||
|
||
|
||
==== tests/cases/conformance/types/conditional/conditionalTypes2.ts (4 errors) ==== | ||
interface Covariant<T> { | ||
foo: T extends string ? T : number; | ||
} | ||
|
||
interface Contravariant<T> { | ||
foo: T extends string ? keyof T : number; | ||
} | ||
|
||
interface Invariant<T> { | ||
foo: T extends string ? keyof T : T; | ||
} | ||
|
||
function f1<A, B extends A>(a: Covariant<A>, b: Covariant<B>) { | ||
a = b; | ||
b = a; // Error | ||
~ | ||
!!! error TS2322: Type 'Covariant<A>' is not assignable to type 'Covariant<B>'. | ||
!!! error TS2322: Type 'A' is not assignable to type 'B'. | ||
} | ||
|
||
function f2<A, B extends A>(a: Contravariant<A>, b: Contravariant<B>) { | ||
a = b; // Error | ||
~ | ||
!!! error TS2322: Type 'Contravariant<B>' is not assignable to type 'Contravariant<A>'. | ||
!!! error TS2322: Type 'A' is not assignable to type 'B'. | ||
b = a; | ||
} | ||
|
||
function f3<A, B extends A>(a: Invariant<A>, b: Invariant<B>) { | ||
a = b; // Error | ||
~ | ||
!!! error TS2322: Type 'Invariant<B>' is not assignable to type 'Invariant<A>'. | ||
!!! error TS2322: Types of property 'foo' are incompatible. | ||
!!! error TS2322: Type 'B extends string ? keyof B : B' is not assignable to type 'A extends string ? keyof A : A'. | ||
!!! error TS2322: Type 'keyof B' is not assignable to type 'keyof A'. | ||
b = a; // Error | ||
~ | ||
!!! error TS2322: Type 'Invariant<A>' is not assignable to type 'Invariant<B>'. | ||
!!! error TS2322: Types of property 'foo' are incompatible. | ||
!!! error TS2322: Type 'A extends string ? keyof A : A' is not assignable to type 'B extends string ? keyof B : B'. | ||
!!! error TS2322: Type 'A' is not assignable to type 'B'. | ||
} | ||
|
||
// Repros from #22860 | ||
|
||
class Opt<T> { | ||
toVector(): Vector<T> { | ||
return <any>undefined; | ||
} | ||
} | ||
|
||
interface Seq<T> { | ||
tail(): Opt<Seq<T>>; | ||
} | ||
|
||
class Vector<T> implements Seq<T> { | ||
tail(): Opt<Vector<T>> { | ||
return <any>undefined; | ||
} | ||
partition2<U extends T>(predicate:(v:T)=>v is U): [Vector<U>,Vector<Exclude<T, U>>]; | ||
partition2(predicate:(x:T)=>boolean): [Vector<T>,Vector<T>]; | ||
partition2<U extends T>(predicate:(v:T)=>boolean): [Vector<U>,Vector<any>] { | ||
return <any>undefined; | ||
} | ||
} | ||
|
||
interface A1<T> { | ||
bat: B1<A1<T>>; | ||
} | ||
|
||
interface B1<T> extends A1<T> { | ||
bat: B1<B1<T>>; | ||
boom: T extends any ? true : true | ||
} | ||
|
This file contains 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,128 @@ | ||
//// [conditionalTypes2.ts] | ||
interface Covariant<T> { | ||
foo: T extends string ? T : number; | ||
} | ||
|
||
interface Contravariant<T> { | ||
foo: T extends string ? keyof T : number; | ||
} | ||
|
||
interface Invariant<T> { | ||
foo: T extends string ? keyof T : T; | ||
} | ||
|
||
function f1<A, B extends A>(a: Covariant<A>, b: Covariant<B>) { | ||
a = b; | ||
b = a; // Error | ||
} | ||
|
||
function f2<A, B extends A>(a: Contravariant<A>, b: Contravariant<B>) { | ||
a = b; // Error | ||
b = a; | ||
} | ||
|
||
function f3<A, B extends A>(a: Invariant<A>, b: Invariant<B>) { | ||
a = b; // Error | ||
b = a; // Error | ||
} | ||
|
||
// Repros from #22860 | ||
|
||
class Opt<T> { | ||
toVector(): Vector<T> { | ||
return <any>undefined; | ||
} | ||
} | ||
|
||
interface Seq<T> { | ||
tail(): Opt<Seq<T>>; | ||
} | ||
|
||
class Vector<T> implements Seq<T> { | ||
tail(): Opt<Vector<T>> { | ||
return <any>undefined; | ||
} | ||
partition2<U extends T>(predicate:(v:T)=>v is U): [Vector<U>,Vector<Exclude<T, U>>]; | ||
partition2(predicate:(x:T)=>boolean): [Vector<T>,Vector<T>]; | ||
partition2<U extends T>(predicate:(v:T)=>boolean): [Vector<U>,Vector<any>] { | ||
return <any>undefined; | ||
} | ||
} | ||
|
||
interface A1<T> { | ||
bat: B1<A1<T>>; | ||
} | ||
|
||
interface B1<T> extends A1<T> { | ||
bat: B1<B1<T>>; | ||
boom: T extends any ? true : true | ||
} | ||
|
||
|
||
//// [conditionalTypes2.js] | ||
"use strict"; | ||
function f1(a, b) { | ||
a = b; | ||
b = a; // Error | ||
} | ||
function f2(a, b) { | ||
a = b; // Error | ||
b = a; | ||
} | ||
function f3(a, b) { | ||
a = b; // Error | ||
b = a; // Error | ||
} | ||
// Repros from #22860 | ||
var Opt = /** @class */ (function () { | ||
function Opt() { | ||
} | ||
Opt.prototype.toVector = function () { | ||
return undefined; | ||
}; | ||
return Opt; | ||
}()); | ||
var Vector = /** @class */ (function () { | ||
function Vector() { | ||
} | ||
Vector.prototype.tail = function () { | ||
return undefined; | ||
}; | ||
Vector.prototype.partition2 = function (predicate) { | ||
return undefined; | ||
}; | ||
return Vector; | ||
}()); | ||
|
||
|
||
//// [conditionalTypes2.d.ts] | ||
interface Covariant<T> { | ||
foo: T extends string ? T : number; | ||
} | ||
interface Contravariant<T> { | ||
foo: T extends string ? keyof T : number; | ||
} | ||
interface Invariant<T> { | ||
foo: T extends string ? keyof T : T; | ||
} | ||
declare function f1<A, B extends A>(a: Covariant<A>, b: Covariant<B>): void; | ||
declare function f2<A, B extends A>(a: Contravariant<A>, b: Contravariant<B>): void; | ||
declare function f3<A, B extends A>(a: Invariant<A>, b: Invariant<B>): void; | ||
declare class Opt<T> { | ||
toVector(): Vector<T>; | ||
} | ||
interface Seq<T> { | ||
tail(): Opt<Seq<T>>; | ||
} | ||
declare class Vector<T> implements Seq<T> { | ||
tail(): Opt<Vector<T>>; | ||
partition2<U extends T>(predicate: (v: T) => v is U): [Vector<U>, Vector<Exclude<T, U>>]; | ||
partition2(predicate: (x: T) => boolean): [Vector<T>, Vector<T>]; | ||
} | ||
interface A1<T> { | ||
bat: B1<A1<T>>; | ||
} | ||
interface B1<T> extends A1<T> { | ||
bat: B1<B1<T>>; | ||
boom: T extends any ? true : true; | ||
} |
Oops, something went wrong.