|
| 1 | +tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(14,22): error TS2345: Argument of type '"value"' is not assignable to parameter of type 'XX extends XX ? "value" : never'. |
| 2 | + Type '"value"' is not assignable to type 'never'. |
| 3 | +tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(32,20): error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'. |
| 4 | +tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(33,21): error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'. |
| 5 | +tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(37,24): error TS2345: Argument of type 'T | null' is not assignable to parameter of type 'null extends T | null ? any : never'. |
| 6 | + Type 'null' is not assignable to type 'null extends T | null ? any : never'. |
| 7 | + Type 'null' is not assignable to type 'never'. |
| 8 | +tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(43,5): error TS2322: Type '{ x: T; y: T; }' is not assignable to type 'T extends T ? { x: T; y: T; } : never'. |
| 9 | + Type '{ x: T; y: T; }' is not assignable to type 'never'. |
| 10 | +tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(50,5): error TS2322: Type 'string' is not assignable to type 'Foo<T>'. |
| 11 | + Type 'string' is not assignable to type '"a"'. |
| 12 | +tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(111,5): error TS2322: Type 'Q' is not assignable to type 'InferBecauseWhyNot<Q>'. |
| 13 | + Type '(arg: any) => any' is not assignable to type 'InferBecauseWhyNot<Q>'. |
| 14 | + Type '(arg: any) => any' is not assignable to type 'never'. |
| 15 | + Type 'Q' is not assignable to type 'never'. |
| 16 | + Type '(arg: any) => any' is not assignable to type 'never'. |
| 17 | +tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(131,1): error TS2322: Type 'true' is not assignable to type 'false'. |
| 18 | +tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(132,1): error TS2322: Type 'false' is not assignable to type 'true'. |
| 19 | +tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(136,1): error TS2322: Type 'false' is not assignable to type 'true'. |
| 20 | +tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(137,1): error TS2322: Type 'true' is not assignable to type 'false'. |
| 21 | +tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(147,5): error TS2322: Type 'T extends "a" ? true : false' is not assignable to type 'false'. |
| 22 | + Type 'boolean' is not assignable to type 'false'. |
| 23 | + Type 'true' is not assignable to type 'false'. |
| 24 | +tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(148,5): error TS2322: Type 'T extends "b" ? true : false' is not assignable to type 'true'. |
| 25 | + Type 'boolean' is not assignable to type 'true'. |
| 26 | + Type 'false' is not assignable to type 'true'. |
| 27 | +tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(152,5): error TS2322: Type 'false' is not assignable to type 'T extends "a" ? true : false'. |
| 28 | + Type 'false' is not assignable to type 'never'. |
| 29 | + Type 'false' is not assignable to type 'true'. |
| 30 | +tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts(153,5): error TS2322: Type 'true' is not assignable to type 'T extends "b" ? true : false'. |
| 31 | + Type 'true' is not assignable to type 'never'. |
| 32 | + Type 'true' is not assignable to type 'false'. |
| 33 | + |
| 34 | + |
| 35 | +==== tests/cases/compiler/conditionalTypeAssignabilityWhenDeferred.ts (15 errors) ==== |
| 36 | + // #29505 |
| 37 | + |
| 38 | + export type FilterPropsByType<T, TT> = { |
| 39 | + [K in keyof T]: T[K] extends TT ? K : never |
| 40 | + }[keyof T]; |
| 41 | + |
| 42 | + function select< |
| 43 | + T extends string | number, |
| 44 | + TList extends object, |
| 45 | + TValueProp extends FilterPropsByType<TList, T> |
| 46 | + >(property: T, list: TList[], valueProp: TValueProp) {} |
| 47 | + |
| 48 | + <XX extends string>(x: XX, tipos: { value: XX }[]) => { |
| 49 | + select(x, tipos, "value"); |
| 50 | + ~~~~~~~ |
| 51 | +!!! error TS2345: Argument of type '"value"' is not assignable to parameter of type 'XX extends XX ? "value" : never'. |
| 52 | +!!! error TS2345: Type '"value"' is not assignable to type 'never'. |
| 53 | + }; |
| 54 | + |
| 55 | + // #29662 |
| 56 | + |
| 57 | + declare function onlyNullablePlease<T extends null extends T ? any : never>( |
| 58 | + value: T |
| 59 | + ): void; |
| 60 | + |
| 61 | + declare function onlyNullablePlease2< |
| 62 | + T extends [null] extends [T] ? any : never |
| 63 | + >(value: T): void; |
| 64 | + |
| 65 | + declare var z: string | null; |
| 66 | + onlyNullablePlease(z); // works as expected |
| 67 | + onlyNullablePlease2(z); // works as expected |
| 68 | + |
| 69 | + declare var y: string; |
| 70 | + onlyNullablePlease(y); // error as expected |
| 71 | + ~ |
| 72 | +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'. |
| 73 | + onlyNullablePlease2(y); // error as expected |
| 74 | + ~ |
| 75 | +!!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'. |
| 76 | + |
| 77 | + <T>(t: T) => { |
| 78 | + var x: T | null = Math.random() > 0.5 ? null : t; |
| 79 | + onlyNullablePlease(x); // should work |
| 80 | + ~ |
| 81 | +!!! error TS2345: Argument of type 'T | null' is not assignable to parameter of type 'null extends T | null ? any : never'. |
| 82 | +!!! error TS2345: Type 'null' is not assignable to type 'null extends T | null ? any : never'. |
| 83 | +!!! error TS2345: Type 'null' is not assignable to type 'never'. |
| 84 | + onlyNullablePlease2(x); // should work |
| 85 | + }; |
| 86 | + |
| 87 | + <T>(t1: { x: T; y: T }, t2: T extends T ? { x: T; y: T } : never) => { |
| 88 | + t1 = t2; // OK |
| 89 | + t2 = t1; // should fail |
| 90 | + ~~ |
| 91 | +!!! error TS2322: Type '{ x: T; y: T; }' is not assignable to type 'T extends T ? { x: T; y: T; } : never'. |
| 92 | +!!! error TS2322: Type '{ x: T; y: T; }' is not assignable to type 'never'. |
| 93 | + }; |
| 94 | + |
| 95 | + type Foo<T> = T extends true ? string : "a"; |
| 96 | + |
| 97 | + <T>(x: Foo<T>, s: string) => { |
| 98 | + x = "a"; // Currently an error, should be ok |
| 99 | + x = s; // Error |
| 100 | + ~ |
| 101 | +!!! error TS2322: Type 'string' is not assignable to type 'Foo<T>'. |
| 102 | +!!! error TS2322: Type 'string' is not assignable to type '"a"'. |
| 103 | + }; |
| 104 | + |
| 105 | + // #26933 |
| 106 | + |
| 107 | + type Distributive<T> = T extends { a: number } ? { a: number } : { b: number }; |
| 108 | + <T>() => { |
| 109 | + const o = { a: 1, b: 2 }; |
| 110 | + const x: [T] extends [string] |
| 111 | + ? { y: number } |
| 112 | + : { a: number; b: number } = undefined!; |
| 113 | + // Simple case: OK |
| 114 | + const o1: [T] extends [number] ? { a: number } : { b: number } = o; |
| 115 | + // Simple case where source happens to be a conditional type: also OK |
| 116 | + const x1: [T] extends [number] |
| 117 | + ? ([T] extends [string] ? { y: number } : { a: number }) |
| 118 | + : ([T] extends [string] ? { y: number } : { b: number }) = x; |
| 119 | + // Infer type parameters: no good |
| 120 | + const o2: [T] extends [[infer U]] ? U : { b: number } = o; |
| 121 | + |
| 122 | + // The next 4 are arguable - if you choose to ignore the `never` distribution case, |
| 123 | + // then they're all good. The `never` case _is_ a bit of an outlier - we say distributive types |
| 124 | + // look approximately like the sum of their branches, but the `never` case bucks that. |
| 125 | + // There's an argument for the result of dumping `never` into a distributive conditional |
| 126 | + // being not `never`, but instead the intersection of the branches - a much more precise bound |
| 127 | + // on that "impossible" input. |
| 128 | + |
| 129 | + // Distributive where T might instantiate to never: no good |
| 130 | + const o3: Distributive<T> = o; |
| 131 | + // Distributive where T & string might instantiate to never: also no good |
| 132 | + const o4: Distributive<T & string> = o; |
| 133 | + // Distributive where {a: T} cannot instantiate to never: OK |
| 134 | + const o5: Distributive<{ a: T }> = o; |
| 135 | + // Distributive where check type is a conditional which returns a non-never type upon instantiation with `never` but can still return never otherwise: no good |
| 136 | + const o6: Distributive<[T] extends [never] ? { a: number } : never> = o; |
| 137 | + }; |
| 138 | + |
| 139 | + type Wrapped<T> = { ___secret: T }; |
| 140 | + type Unwrap<T> = T extends Wrapped<infer U> ? U : T; |
| 141 | + |
| 142 | + declare function set<T, K extends keyof T>( |
| 143 | + obj: T, |
| 144 | + key: K, |
| 145 | + value: Unwrap<T[K]> |
| 146 | + ): Unwrap<T[K]>; |
| 147 | + |
| 148 | + class Foo2 { |
| 149 | + prop!: Wrapped<string>; |
| 150 | + |
| 151 | + method() { |
| 152 | + set(this, "prop", "hi"); // <-- type error |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + set(new Foo2(), "prop", "hi"); // <-- typechecks |
| 157 | + |
| 158 | + type InferBecauseWhyNot<T> = [T] extends [(p: infer P1) => any] |
| 159 | + ? P1 | T |
| 160 | + : never; |
| 161 | + |
| 162 | + <Q extends (arg: any) => any>(x: Q): InferBecauseWhyNot<Q> => { |
| 163 | + return x; |
| 164 | + ~~~~~~~~~ |
| 165 | +!!! error TS2322: Type 'Q' is not assignable to type 'InferBecauseWhyNot<Q>'. |
| 166 | +!!! error TS2322: Type '(arg: any) => any' is not assignable to type 'InferBecauseWhyNot<Q>'. |
| 167 | +!!! error TS2322: Type '(arg: any) => any' is not assignable to type 'never'. |
| 168 | +!!! error TS2322: Type 'Q' is not assignable to type 'never'. |
| 169 | +!!! error TS2322: Type '(arg: any) => any' is not assignable to type 'never'. |
| 170 | + }; |
| 171 | + |
| 172 | + type InferBecauseWhyNotDistributive<T> = T extends (p: infer P1) => any |
| 173 | + ? P1 | T |
| 174 | + : never; |
| 175 | + |
| 176 | + <Q extends (arg: any) => any>( |
| 177 | + x: Q |
| 178 | + ): InferBecauseWhyNotDistributive<Q> => { |
| 179 | + return x; // should fail |
| 180 | + }; |
| 181 | + |
| 182 | + let t: true; |
| 183 | + let f: false; |
| 184 | + |
| 185 | + let a: "a" extends "a" ? true : false = undefined!; |
| 186 | + let b: "a" extends "b" ? true : false = undefined!; |
| 187 | + |
| 188 | + t = a; |
| 189 | + f = a; // !!! error TS2322: Type 'true' is not assignable to type 'false'. |
| 190 | + ~ |
| 191 | +!!! error TS2322: Type 'true' is not assignable to type 'false'. |
| 192 | + t = b; // !!! error TS2322: Type 'false' is not assignable to type 'true'. |
| 193 | + ~ |
| 194 | +!!! error TS2322: Type 'false' is not assignable to type 'true'. |
| 195 | + f = b; |
| 196 | + |
| 197 | + a = true; |
| 198 | + a = false; // !!! error TS2322: Type 'false' is not assignable to type 'true'. |
| 199 | + ~ |
| 200 | +!!! error TS2322: Type 'false' is not assignable to type 'true'. |
| 201 | + b = true; // !!! error TS2322: Type 'true' is not assignable to type 'false'. |
| 202 | + ~ |
| 203 | +!!! error TS2322: Type 'true' is not assignable to type 'false'. |
| 204 | + b = false; |
| 205 | + |
| 206 | + // #23132 |
| 207 | + |
| 208 | + <T extends "a">() => { |
| 209 | + let a: T extends "a" ? true : false = undefined!; |
| 210 | + let b: T extends "b" ? true : false = undefined!; |
| 211 | + |
| 212 | + t = a; |
| 213 | + f = a; // !!! error TS2322: Type 'T extends "a" ? true : false' is not assignable to type 'false'. |
| 214 | + ~ |
| 215 | +!!! error TS2322: Type 'T extends "a" ? true : false' is not assignable to type 'false'. |
| 216 | +!!! error TS2322: Type 'boolean' is not assignable to type 'false'. |
| 217 | +!!! error TS2322: Type 'true' is not assignable to type 'false'. |
| 218 | + t = b; // !!! error TS2322: Type 'T extends "b" ? true : false' is not assignable to type 'true'. |
| 219 | + ~ |
| 220 | +!!! error TS2322: Type 'T extends "b" ? true : false' is not assignable to type 'true'. |
| 221 | +!!! error TS2322: Type 'boolean' is not assignable to type 'true'. |
| 222 | +!!! error TS2322: Type 'false' is not assignable to type 'true'. |
| 223 | + f = b; |
| 224 | + |
| 225 | + a = true; |
| 226 | + a = false; // !!! error TS2322: Type 'false' is not assignable to type 'T extends "a" ? true : false'. |
| 227 | + ~ |
| 228 | +!!! error TS2322: Type 'false' is not assignable to type 'T extends "a" ? true : false'. |
| 229 | +!!! error TS2322: Type 'false' is not assignable to type 'never'. |
| 230 | +!!! error TS2322: Type 'false' is not assignable to type 'true'. |
| 231 | + b = true; // !!! error TS2322: Type 'true' is not assignable to type 'T extends "b" ? true : false'. |
| 232 | + ~ |
| 233 | +!!! error TS2322: Type 'true' is not assignable to type 'T extends "b" ? true : false'. |
| 234 | +!!! error TS2322: Type 'true' is not assignable to type 'never'. |
| 235 | +!!! error TS2322: Type 'true' is not assignable to type 'false'. |
| 236 | + b = false; |
| 237 | + }; |
| 238 | + |
0 commit comments