|
| 1 | +//// [inDoesNotOperateOnPrimitiveTypes.ts] |
| 2 | +const validHasKey = <T extends object>( |
| 3 | + thing: T, |
| 4 | + key: string, |
| 5 | +): boolean => { |
| 6 | + return key in thing; // Ok |
| 7 | +}; |
| 8 | + |
| 9 | +const alsoValidHasKey = <T>( |
| 10 | + thing: T, |
| 11 | + key: string, |
| 12 | +): boolean => { |
| 13 | + return key in thing; // Ok (as T may be instantiated with a valid type) |
| 14 | +}; |
| 15 | + |
| 16 | +function invalidHasKey<T extends string | number>( |
| 17 | + thing: T, |
| 18 | + key: string, |
| 19 | +): boolean { |
| 20 | + return key in thing; // Error (because all possible instantiations are errors) |
| 21 | +} |
| 22 | + |
| 23 | +function union1<T extends string | number, U extends boolean>(thing: T | U) { |
| 24 | + "key" in thing; // Error (because all possible instantiations are errors) |
| 25 | +} |
| 26 | + |
| 27 | +function union2<T extends object, U extends string | number>(thing: T | U) { |
| 28 | + "key" in thing; // Error (because narrowing is possible) |
| 29 | + if (typeof thing === "object") { |
| 30 | + "key" in thing; // Ok |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +function union3<T>(thing: T | string | number) { |
| 35 | + "key" in thing; // Error (because narrowing is possible) |
| 36 | + if (typeof thing !== "string" && typeof thing !== "number") { |
| 37 | + "key" in thing; // Ok (because further narrowing is impossible) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +function union4<T extends object | "hello">(thing: T) { |
| 42 | + "key" in thing; // Ok (because narrowing is impossible) |
| 43 | +} |
| 44 | + |
| 45 | +function union5<T extends object | string, U extends object | number>(p: T | U) { |
| 46 | + // For consistency, this should probably not be an error, because useful |
| 47 | + // narrowing is impossible. However, this is exceptionally strange input, |
| 48 | + // and it adds a lot of complexity to distinguish between a `T | U` where |
| 49 | + // one constraint is non-primitive and the other is primitive and a `T | U` |
| 50 | + // like this where both constraints have primitive and non-primitive |
| 51 | + // constitutents. Also, the strictly sound behavior would be to error |
| 52 | + // here, which is what's happening, so "fixing" this by suppressing the |
| 53 | + // error seems very low-value. |
| 54 | + "key" in p; |
| 55 | + if (typeof p === "object") { |
| 56 | + "key" in p; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +function intersection1<T extends number, U extends 0 | 1 | 2>(thing: T & U) { |
| 61 | + "key" in thing; // Error (because all possible instantiations are errors) |
| 62 | +} |
| 63 | + |
| 64 | +function intersection2<T>(thing: T & (0 | 1 | 2)) { |
| 65 | + "key" in thing; // Error (because all possible instantations are errors) |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | +//// [inDoesNotOperateOnPrimitiveTypes.js] |
| 70 | +var validHasKey = function (thing, key) { |
| 71 | + return key in thing; // Ok |
| 72 | +}; |
| 73 | +var alsoValidHasKey = function (thing, key) { |
| 74 | + return key in thing; // Ok (as T may be instantiated with a valid type) |
| 75 | +}; |
| 76 | +function invalidHasKey(thing, key) { |
| 77 | + return key in thing; // Error (because all possible instantiations are errors) |
| 78 | +} |
| 79 | +function union1(thing) { |
| 80 | + "key" in thing; // Error (because all possible instantiations are errors) |
| 81 | +} |
| 82 | +function union2(thing) { |
| 83 | + "key" in thing; // Error (because narrowing is possible) |
| 84 | + if (typeof thing === "object") { |
| 85 | + "key" in thing; // Ok |
| 86 | + } |
| 87 | +} |
| 88 | +function union3(thing) { |
| 89 | + "key" in thing; // Error (because narrowing is possible) |
| 90 | + if (typeof thing !== "string" && typeof thing !== "number") { |
| 91 | + "key" in thing; // Ok (because further narrowing is impossible) |
| 92 | + } |
| 93 | +} |
| 94 | +function union4(thing) { |
| 95 | + "key" in thing; // Ok (because narrowing is impossible) |
| 96 | +} |
| 97 | +function union5(p) { |
| 98 | + // For consistency, this should probably not be an error, because useful |
| 99 | + // narrowing is impossible. However, this is exceptionally strange input, |
| 100 | + // and it adds a lot of complexity to distinguish between a `T | U` where |
| 101 | + // one constraint is non-primitive and the other is primitive and a `T | U` |
| 102 | + // like this where both constraints have primitive and non-primitive |
| 103 | + // constitutents. Also, the strictly sound behavior would be to error |
| 104 | + // here, which is what's happening, so "fixing" this by suppressing the |
| 105 | + // error seems very low-value. |
| 106 | + "key" in p; |
| 107 | + if (typeof p === "object") { |
| 108 | + "key" in p; |
| 109 | + } |
| 110 | +} |
| 111 | +function intersection1(thing) { |
| 112 | + "key" in thing; // Error (because all possible instantiations are errors) |
| 113 | +} |
| 114 | +function intersection2(thing) { |
| 115 | + "key" in thing; // Error (because all possible instantations are errors) |
| 116 | +} |
0 commit comments