|
| 1 | +tests/cases/compiler/varianceMeasurement.ts(10,7): error TS2322: Type 'Foo1<string>' is not assignable to type 'Foo1<"a">'. |
| 2 | + Type 'string' is not assignable to type '"a"'. |
| 3 | +tests/cases/compiler/varianceMeasurement.ts(21,7): error TS2322: Type 'Foo2<string>' is not assignable to type 'Foo2<"a">'. |
| 4 | + Types of property 'x' are incompatible. |
| 5 | + Type 'string' is not assignable to type '"a"'. |
| 6 | +tests/cases/compiler/varianceMeasurement.ts(22,7): error TS2322: Type 'Foo2<string>' is not assignable to type 'Foo2<unknown>'. |
| 7 | + The types of 'y.x' are incompatible between these types. |
| 8 | + Type '(arg: string) => void' is not assignable to type '(arg: unknown) => void'. |
| 9 | + Types of parameters 'arg' and 'arg' are incompatible. |
| 10 | + Type 'unknown' is not assignable to type 'string'. |
| 11 | +tests/cases/compiler/varianceMeasurement.ts(33,7): error TS2322: Type 'Foo3<string>' is not assignable to type 'Foo3<"a">'. |
| 12 | + Type 'string' is not assignable to type '"a"'. |
| 13 | +tests/cases/compiler/varianceMeasurement.ts(44,7): error TS2322: Type 'Foo4<string>' is not assignable to type 'Foo4<"a">'. |
| 14 | + Types of property 'x' are incompatible. |
| 15 | + Type 'string' is not assignable to type '"a"'. |
| 16 | +tests/cases/compiler/varianceMeasurement.ts(45,7): error TS2322: Type 'Foo4<string>' is not assignable to type 'Foo4<unknown>'. |
| 17 | + The types of 'y.x' are incompatible between these types. |
| 18 | + Type '(arg: string) => void' is not assignable to type '(arg: unknown) => void'. |
| 19 | + Types of parameters 'arg' and 'arg' are incompatible. |
| 20 | + Type 'unknown' is not assignable to type 'string'. |
| 21 | +tests/cases/compiler/varianceMeasurement.ts(57,7): error TS2322: Type 'Fn<string, number>' is not assignable to type 'Fn<unknown, number>'. |
| 22 | + Type 'unknown' is not assignable to type 'string'. |
| 23 | +tests/cases/compiler/varianceMeasurement.ts(62,7): error TS2322: Type 'Fn<string, number>' is not assignable to type 'Fn<string, 0>'. |
| 24 | + Type 'number' is not assignable to type '0'. |
| 25 | + |
| 26 | + |
| 27 | +==== tests/cases/compiler/varianceMeasurement.ts (8 errors) ==== |
| 28 | + // The type below should be invariant in T but is measured as covariant because |
| 29 | + // we don't analyze recursive references. |
| 30 | + |
| 31 | + interface Foo1<T> { |
| 32 | + x: T; |
| 33 | + y: Foo1<(arg: T) => void>; |
| 34 | + } |
| 35 | + |
| 36 | + declare const f10: Foo1<string>; |
| 37 | + const f11: Foo1<'a'> = f10; |
| 38 | + ~~~ |
| 39 | +!!! error TS2322: Type 'Foo1<string>' is not assignable to type 'Foo1<"a">'. |
| 40 | +!!! error TS2322: Type 'string' is not assignable to type '"a"'. |
| 41 | + const f12: Foo1<unknown> = f10; |
| 42 | + |
| 43 | + // The type below is invariant in T and is measured as such. |
| 44 | + |
| 45 | + interface Foo2<T> { |
| 46 | + x: T; |
| 47 | + y: { x: (arg: T) => void, y: Foo2<(arg: T) => void>; } |
| 48 | + } |
| 49 | + |
| 50 | + declare const f20: Foo2<string>; |
| 51 | + const f21: Foo2<'a'> = f20; |
| 52 | + ~~~ |
| 53 | +!!! error TS2322: Type 'Foo2<string>' is not assignable to type 'Foo2<"a">'. |
| 54 | +!!! error TS2322: Types of property 'x' are incompatible. |
| 55 | +!!! error TS2322: Type 'string' is not assignable to type '"a"'. |
| 56 | + const f22: Foo2<unknown> = f20; |
| 57 | + ~~~ |
| 58 | +!!! error TS2322: Type 'Foo2<string>' is not assignable to type 'Foo2<unknown>'. |
| 59 | +!!! error TS2322: The types of 'y.x' are incompatible between these types. |
| 60 | +!!! error TS2322: Type '(arg: string) => void' is not assignable to type '(arg: unknown) => void'. |
| 61 | +!!! error TS2322: Types of parameters 'arg' and 'arg' are incompatible. |
| 62 | +!!! error TS2322: Type 'unknown' is not assignable to type 'string'. |
| 63 | + |
| 64 | + // The type below should be invariant in T but is measured as covariant because |
| 65 | + // we don't analyze recursive references. |
| 66 | + |
| 67 | + type Foo3<T> = { |
| 68 | + x: T; |
| 69 | + y: Foo3<(arg: T) => void>; |
| 70 | + } |
| 71 | + |
| 72 | + declare const f30: Foo3<string>; |
| 73 | + const f31: Foo3<'a'> = f30; |
| 74 | + ~~~ |
| 75 | +!!! error TS2322: Type 'Foo3<string>' is not assignable to type 'Foo3<"a">'. |
| 76 | +!!! error TS2322: Type 'string' is not assignable to type '"a"'. |
| 77 | + const f32: Foo3<unknown> = f30; |
| 78 | + |
| 79 | + // The type below is invariant in T and is measured as such. |
| 80 | + |
| 81 | + type Foo4<T> = { |
| 82 | + x: T; |
| 83 | + y: { x: (arg: T) => void, y: Foo4<(arg: T) => void>; } |
| 84 | + } |
| 85 | + |
| 86 | + declare const f40: Foo4<string>; |
| 87 | + const f41: Foo4<'a'> = f40; |
| 88 | + ~~~ |
| 89 | +!!! error TS2322: Type 'Foo4<string>' is not assignable to type 'Foo4<"a">'. |
| 90 | +!!! error TS2322: Types of property 'x' are incompatible. |
| 91 | +!!! error TS2322: Type 'string' is not assignable to type '"a"'. |
| 92 | + const f42: Foo4<unknown> = f40; |
| 93 | + ~~~ |
| 94 | +!!! error TS2322: Type 'Foo4<string>' is not assignable to type 'Foo4<unknown>'. |
| 95 | +!!! error TS2322: The types of 'y.x' are incompatible between these types. |
| 96 | +!!! error TS2322: Type '(arg: string) => void' is not assignable to type '(arg: unknown) => void'. |
| 97 | +!!! error TS2322: Types of parameters 'arg' and 'arg' are incompatible. |
| 98 | +!!! error TS2322: Type 'unknown' is not assignable to type 'string'. |
| 99 | + |
| 100 | + // Repro from #3580 |
| 101 | + |
| 102 | + interface Fn<A, B> { |
| 103 | + (a: A): B; |
| 104 | + then<C>(next: Fn<B, C>): Fn<A, C>; |
| 105 | + } |
| 106 | + |
| 107 | + declare const fn: Fn<string, number>; |
| 108 | + |
| 109 | + // Contravariant in A |
| 110 | + const fn1: Fn<unknown, number> = fn; // Error |
| 111 | + ~~~ |
| 112 | +!!! error TS2322: Type 'Fn<string, number>' is not assignable to type 'Fn<unknown, number>'. |
| 113 | +!!! error TS2322: Type 'unknown' is not assignable to type 'string'. |
| 114 | + const fn2: Fn<'a', number> = fn; |
| 115 | + |
| 116 | + // Covariant in B |
| 117 | + const fn3: Fn<string, unknown> = fn; |
| 118 | + const fn4: Fn<string, 0> = fn; // Error |
| 119 | + ~~~ |
| 120 | +!!! error TS2322: Type 'Fn<string, number>' is not assignable to type 'Fn<string, 0>'. |
| 121 | +!!! error TS2322: Type 'number' is not assignable to type '0'. |
| 122 | + |
0 commit comments