From 3d04388907f0433dc4d957790520c440f48e33a7 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Sun, 25 Mar 2018 11:30:08 -0700 Subject: [PATCH] Add tests --- .../types/conditional/conditionalTypes2.ts | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 tests/cases/conformance/types/conditional/conditionalTypes2.ts diff --git a/tests/cases/conformance/types/conditional/conditionalTypes2.ts b/tests/cases/conformance/types/conditional/conditionalTypes2.ts new file mode 100644 index 00000000000..d08700a0119 --- /dev/null +++ b/tests/cases/conformance/types/conditional/conditionalTypes2.ts @@ -0,0 +1,65 @@ +// @strict: true +// @declaration: true + +interface Covariant { + foo: T extends string ? T : number; +} + +interface Contravariant { + foo: T extends string ? keyof T : number; +} + +interface Invariant { + foo: T extends string ? keyof T : T; +} + +interface A { a: string } +interface B extends A { b: string } + + +function f1(a: Covariant, b: Covariant) { + a = b; + b = a; // Error +} + +function f2(a: Contravariant, b: Contravariant) { + a = b; // Error + b = a; +} + +function f3(a: Invariant, b: Invariant) { + a = b; // Error + b = a; // Error +} + +// Repros from #22860 + +export class Option { + toVector(): Vector { + return undefined; + } +} + +interface Seq { + tail(): Option>; +} + +class Vector implements Seq { + tail(): Option> { + return undefined; + } + partition2(predicate:(v:T)=>v is U): [Vector,Vector>]; + partition2(predicate:(x:T)=>boolean): [Vector,Vector]; + partition2(predicate:(v:T)=>boolean): [Vector,Vector] { + return undefined; + } +} + +interface A1 { + bat: B1>; +} + +interface B1 extends A1 { + bat: B1>; + boom: T extends any ? true : true +}