|
| 1 | +//// [conditionalTypeAssignabilityWhenDeferred.ts] |
| 2 | +export type FilterPropsByType<T, TT> = { |
| 3 | + [K in keyof T]: T[K] extends TT ? K : never |
| 4 | +}[keyof T]; |
| 5 | + |
| 6 | +function select< |
| 7 | + T extends string | number, |
| 8 | + TList extends object, |
| 9 | + TValueProp extends FilterPropsByType<TList, T> |
| 10 | +>(property: T, list: TList[], valueProp: TValueProp) {} |
| 11 | + |
| 12 | +export function func<XX extends string>(x: XX, tipos: { value: XX }[]) { |
| 13 | + select(x, tipos, "value"); |
| 14 | +} |
| 15 | + |
| 16 | +declare function onlyNullablePlease<T extends null extends T ? any : never>( |
| 17 | + value: T |
| 18 | +): void; |
| 19 | + |
| 20 | +declare function onlyNullablePlease2< |
| 21 | + T extends [null] extends [T] ? any : never |
| 22 | +>(value: T): void; |
| 23 | + |
| 24 | +declare var z: string | null; |
| 25 | +onlyNullablePlease(z); // works as expected |
| 26 | +onlyNullablePlease2(z); // works as expected |
| 27 | + |
| 28 | +declare var y: string; |
| 29 | +onlyNullablePlease(y); // error as expected |
| 30 | +onlyNullablePlease2(y); // error as expected |
| 31 | + |
| 32 | +function f<T>(t: T) { |
| 33 | + var x: T | null = Math.random() > 0.5 ? null : t; |
| 34 | + onlyNullablePlease(x); // should work |
| 35 | + onlyNullablePlease2(x); // should work |
| 36 | +} |
| 37 | + |
| 38 | +function f2<T>(t1: { x: T; y: T }, t2: T extends T ? { x: T; y: T } : never) { |
| 39 | + t1 = t2; // OK |
| 40 | + t2 = t1; // should fail |
| 41 | +} |
| 42 | + |
| 43 | +type Foo<T> = T extends true ? string : "a"; |
| 44 | + |
| 45 | +function test<T>(x: Foo<T>, s: string) { |
| 46 | + x = "a"; // Currently an error, should be ok |
| 47 | + x = s; // Error |
| 48 | +} |
| 49 | + |
| 50 | +// #26933 |
| 51 | +type Distributive<T> = T extends { a: number } ? { a: number } : { b: number }; |
| 52 | +function testAssignabilityToConditionalType<T>() { |
| 53 | + const o = { a: 1, b: 2 }; |
| 54 | + const x: [T] extends [string] |
| 55 | + ? { y: number } |
| 56 | + : { a: number; b: number } = undefined!; |
| 57 | + // Simple case: OK |
| 58 | + const o1: [T] extends [number] ? { a: number } : { b: number } = o; |
| 59 | + // Simple case where source happens to be a conditional type: also OK |
| 60 | + const x1: [T] extends [number] |
| 61 | + ? ([T] extends [string] ? { y: number } : { a: number }) |
| 62 | + : ([T] extends [string] ? { y: number } : { b: number }) = x; |
| 63 | + // Infer type parameters: no good |
| 64 | + const o2: [T] extends [[infer U]] ? U : { b: number } = o; |
| 65 | + |
| 66 | + // The next 4 are arguable - if you choose to ignore the `never` distribution case, |
| 67 | + // then they're all good. The `never` case _is_ a bit of an outlier - we say distributive types |
| 68 | + // look approximately like the sum of their branches, but the `never` case bucks that. |
| 69 | + // There's an argument for the result of dumping `never` into a distributive conditional |
| 70 | + // being not `never`, but instead the intersection of the branches - a much more precise bound |
| 71 | + // on that "impossible" input. |
| 72 | + |
| 73 | + // Distributive where T might instantiate to never: no good |
| 74 | + const o3: Distributive<T> = o; |
| 75 | + // Distributive where T & string might instantiate to never: also no good |
| 76 | + const o4: Distributive<T & string> = o; |
| 77 | + // Distributive where {a: T} cannot instantiate to never: OK |
| 78 | + const o5: Distributive<{ a: T }> = o; |
| 79 | + // 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 |
| 80 | + const o6: Distributive<[T] extends [never] ? { a: number } : never> = o; |
| 81 | +} |
| 82 | + |
| 83 | +type Wrapped<T> = { ___secret: T }; |
| 84 | +type Unwrap<T> = T extends Wrapped<infer U> ? U : T; |
| 85 | + |
| 86 | +declare function set<T, K extends keyof T>( |
| 87 | + obj: T, |
| 88 | + key: K, |
| 89 | + value: Unwrap<T[K]> |
| 90 | +): Unwrap<T[K]>; |
| 91 | + |
| 92 | +class Foo2 { |
| 93 | + prop!: Wrapped<string>; |
| 94 | + |
| 95 | + method() { |
| 96 | + set(this, "prop", "hi"); // <-- type error |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +set(new Foo2(), "prop", "hi"); // <-- typechecks |
| 101 | + |
| 102 | +type InferBecauseWhyNot<T> = [T] extends [(p: infer P1) => any] |
| 103 | + ? P1 | T |
| 104 | + : never; |
| 105 | + |
| 106 | +function f3<Q extends (arg: any) => any>(x: Q): InferBecauseWhyNot<Q> { |
| 107 | + return x; |
| 108 | +} |
| 109 | + |
| 110 | +type InferBecauseWhyNotDistributive<T> = T extends (p: infer P1) => any |
| 111 | + ? P1 | T |
| 112 | + : never; |
| 113 | + |
| 114 | +function f4<Q extends (arg: any) => any>( |
| 115 | + x: Q |
| 116 | +): InferBecauseWhyNotDistributive<Q> { |
| 117 | + return x; // should fail |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | +//// [conditionalTypeAssignabilityWhenDeferred.js] |
| 122 | +"use strict"; |
| 123 | +exports.__esModule = true; |
| 124 | +exports.func = void 0; |
| 125 | +function select(property, list, valueProp) { } |
| 126 | +function func(x, tipos) { |
| 127 | + select(x, tipos, "value"); |
| 128 | +} |
| 129 | +exports.func = func; |
| 130 | +onlyNullablePlease(z); // works as expected |
| 131 | +onlyNullablePlease2(z); // works as expected |
| 132 | +onlyNullablePlease(y); // error as expected |
| 133 | +onlyNullablePlease2(y); // error as expected |
| 134 | +function f(t) { |
| 135 | + var x = Math.random() > 0.5 ? null : t; |
| 136 | + onlyNullablePlease(x); // should work |
| 137 | + onlyNullablePlease2(x); // should work |
| 138 | +} |
| 139 | +function f2(t1, t2) { |
| 140 | + t1 = t2; // OK |
| 141 | + t2 = t1; // should fail |
| 142 | +} |
| 143 | +function test(x, s) { |
| 144 | + x = "a"; // Currently an error, should be ok |
| 145 | + x = s; // Error |
| 146 | +} |
| 147 | +function testAssignabilityToConditionalType() { |
| 148 | + var o = { a: 1, b: 2 }; |
| 149 | + var x = undefined; |
| 150 | + // Simple case: OK |
| 151 | + var o1 = o; |
| 152 | + // Simple case where source happens to be a conditional type: also OK |
| 153 | + var x1 = x; |
| 154 | + // Infer type parameters: no good |
| 155 | + var o2 = o; |
| 156 | + // The next 4 are arguable - if you choose to ignore the `never` distribution case, |
| 157 | + // then they're all good. The `never` case _is_ a bit of an outlier - we say distributive types |
| 158 | + // look approximately like the sum of their branches, but the `never` case bucks that. |
| 159 | + // There's an argument for the result of dumping `never` into a distributive conditional |
| 160 | + // being not `never`, but instead the intersection of the branches - a much more precise bound |
| 161 | + // on that "impossible" input. |
| 162 | + // Distributive where T might instantiate to never: no good |
| 163 | + var o3 = o; |
| 164 | + // Distributive where T & string might instantiate to never: also no good |
| 165 | + var o4 = o; |
| 166 | + // Distributive where {a: T} cannot instantiate to never: OK |
| 167 | + var o5 = o; |
| 168 | + // 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 |
| 169 | + var o6 = o; |
| 170 | +} |
| 171 | +var Foo2 = /** @class */ (function () { |
| 172 | + function Foo2() { |
| 173 | + } |
| 174 | + Foo2.prototype.method = function () { |
| 175 | + set(this, "prop", "hi"); // <-- type error |
| 176 | + }; |
| 177 | + return Foo2; |
| 178 | +}()); |
| 179 | +set(new Foo2(), "prop", "hi"); // <-- typechecks |
| 180 | +function f3(x) { |
| 181 | + return x; |
| 182 | +} |
| 183 | +function f4(x) { |
| 184 | + return x; // should fail |
| 185 | +} |
0 commit comments