|
| 1 | +=== tests/cases/compiler/narrowingMutualSubtypes.ts === |
| 2 | +// Check that `any` is a strict supertype of `unknown` |
| 3 | + |
| 4 | +declare const ru1: { [x: string]: unknown }; |
| 5 | +>ru1 : Symbol(ru1, Decl(narrowingMutualSubtypes.ts, 2, 13)) |
| 6 | +>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 2, 22)) |
| 7 | + |
| 8 | +declare const ra1: { [x: string]: any }; |
| 9 | +>ra1 : Symbol(ra1, Decl(narrowingMutualSubtypes.ts, 3, 13)) |
| 10 | +>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 3, 22)) |
| 11 | + |
| 12 | +const a1a = [ru1, ra1]; // { [x: string]: any }[] |
| 13 | +>a1a : Symbol(a1a, Decl(narrowingMutualSubtypes.ts, 5, 5)) |
| 14 | +>ru1 : Symbol(ru1, Decl(narrowingMutualSubtypes.ts, 2, 13)) |
| 15 | +>ra1 : Symbol(ra1, Decl(narrowingMutualSubtypes.ts, 3, 13)) |
| 16 | + |
| 17 | +const a1b = [ra1, ru1]; // { [x: string]: any }[] |
| 18 | +>a1b : Symbol(a1b, Decl(narrowingMutualSubtypes.ts, 6, 5)) |
| 19 | +>ra1 : Symbol(ra1, Decl(narrowingMutualSubtypes.ts, 3, 13)) |
| 20 | +>ru1 : Symbol(ru1, Decl(narrowingMutualSubtypes.ts, 2, 13)) |
| 21 | + |
| 22 | +declare const ra2: { [x: string]: any }; |
| 23 | +>ra2 : Symbol(ra2, Decl(narrowingMutualSubtypes.ts, 8, 13)) |
| 24 | +>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 8, 22)) |
| 25 | + |
| 26 | +declare const ru2: { [x: string]: unknown }; |
| 27 | +>ru2 : Symbol(ru2, Decl(narrowingMutualSubtypes.ts, 9, 13)) |
| 28 | +>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 9, 22)) |
| 29 | + |
| 30 | +const a2a = [ru2, ra2]; // { [x: string]: any }[] |
| 31 | +>a2a : Symbol(a2a, Decl(narrowingMutualSubtypes.ts, 11, 5)) |
| 32 | +>ru2 : Symbol(ru2, Decl(narrowingMutualSubtypes.ts, 9, 13)) |
| 33 | +>ra2 : Symbol(ra2, Decl(narrowingMutualSubtypes.ts, 8, 13)) |
| 34 | + |
| 35 | +const a2b = [ra2, ru2]; // { [x: string]: any }[] |
| 36 | +>a2b : Symbol(a2b, Decl(narrowingMutualSubtypes.ts, 12, 5)) |
| 37 | +>ra2 : Symbol(ra2, Decl(narrowingMutualSubtypes.ts, 8, 13)) |
| 38 | +>ru2 : Symbol(ru2, Decl(narrowingMutualSubtypes.ts, 9, 13)) |
| 39 | + |
| 40 | +// Check that `{}` is strict supertype of any non-empty object |
| 41 | + |
| 42 | +const c3 = {}; |
| 43 | +>c3 : Symbol(c3, Decl(narrowingMutualSubtypes.ts, 16, 5)) |
| 44 | + |
| 45 | +declare const r3: { [x: string]: unknown } |
| 46 | +>r3 : Symbol(r3, Decl(narrowingMutualSubtypes.ts, 17, 13)) |
| 47 | +>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 17, 21)) |
| 48 | + |
| 49 | +const a3a = [c3, r3]; // {}[] |
| 50 | +>a3a : Symbol(a3a, Decl(narrowingMutualSubtypes.ts, 19, 5)) |
| 51 | +>c3 : Symbol(c3, Decl(narrowingMutualSubtypes.ts, 16, 5)) |
| 52 | +>r3 : Symbol(r3, Decl(narrowingMutualSubtypes.ts, 17, 13)) |
| 53 | + |
| 54 | +const a3b = [r3, c3]; // {}[] |
| 55 | +>a3b : Symbol(a3b, Decl(narrowingMutualSubtypes.ts, 20, 5)) |
| 56 | +>r3 : Symbol(r3, Decl(narrowingMutualSubtypes.ts, 17, 13)) |
| 57 | +>c3 : Symbol(c3, Decl(narrowingMutualSubtypes.ts, 16, 5)) |
| 58 | + |
| 59 | +declare const r4: { [x: string]: unknown } |
| 60 | +>r4 : Symbol(r4, Decl(narrowingMutualSubtypes.ts, 22, 13)) |
| 61 | +>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 22, 21)) |
| 62 | + |
| 63 | +const c4 = {}; |
| 64 | +>c4 : Symbol(c4, Decl(narrowingMutualSubtypes.ts, 23, 5)) |
| 65 | + |
| 66 | +const a4a = [c4, r4]; // {}[] |
| 67 | +>a4a : Symbol(a4a, Decl(narrowingMutualSubtypes.ts, 25, 5)) |
| 68 | +>c4 : Symbol(c4, Decl(narrowingMutualSubtypes.ts, 23, 5)) |
| 69 | +>r4 : Symbol(r4, Decl(narrowingMutualSubtypes.ts, 22, 13)) |
| 70 | + |
| 71 | +const a4b = [r4, c4]; // {}[] |
| 72 | +>a4b : Symbol(a4b, Decl(narrowingMutualSubtypes.ts, 26, 5)) |
| 73 | +>r4 : Symbol(r4, Decl(narrowingMutualSubtypes.ts, 22, 13)) |
| 74 | +>c4 : Symbol(c4, Decl(narrowingMutualSubtypes.ts, 23, 5)) |
| 75 | + |
| 76 | +// Check that narrowing preserves original type in false branch for non-identical mutual subtypes |
| 77 | + |
| 78 | +declare function isObject1(value: unknown): value is Record<string, unknown>; |
| 79 | +>isObject1 : Symbol(isObject1, Decl(narrowingMutualSubtypes.ts, 26, 21)) |
| 80 | +>value : Symbol(value, Decl(narrowingMutualSubtypes.ts, 30, 27)) |
| 81 | +>value : Symbol(value, Decl(narrowingMutualSubtypes.ts, 30, 27)) |
| 82 | +>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) |
| 83 | + |
| 84 | +function gg(x: {}) { |
| 85 | +>gg : Symbol(gg, Decl(narrowingMutualSubtypes.ts, 30, 77)) |
| 86 | +>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 32, 12)) |
| 87 | + |
| 88 | + if (isObject1(x)) { |
| 89 | +>isObject1 : Symbol(isObject1, Decl(narrowingMutualSubtypes.ts, 26, 21)) |
| 90 | +>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 32, 12)) |
| 91 | + |
| 92 | + x; // Record<string, unknown> |
| 93 | +>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 32, 12)) |
| 94 | + } |
| 95 | + else { |
| 96 | + x; // {} |
| 97 | +>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 32, 12)) |
| 98 | + } |
| 99 | + x; // {} |
| 100 | +>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 32, 12)) |
| 101 | +} |
| 102 | + |
| 103 | +declare function isObject2(value: unknown): value is {}; |
| 104 | +>isObject2 : Symbol(isObject2, Decl(narrowingMutualSubtypes.ts, 40, 1)) |
| 105 | +>value : Symbol(value, Decl(narrowingMutualSubtypes.ts, 42, 27)) |
| 106 | +>value : Symbol(value, Decl(narrowingMutualSubtypes.ts, 42, 27)) |
| 107 | + |
| 108 | +function gg2(x: Record<string, unknown>) { |
| 109 | +>gg2 : Symbol(gg2, Decl(narrowingMutualSubtypes.ts, 42, 56)) |
| 110 | +>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 44, 13)) |
| 111 | +>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) |
| 112 | + |
| 113 | + if (isObject2(x)) { |
| 114 | +>isObject2 : Symbol(isObject2, Decl(narrowingMutualSubtypes.ts, 40, 1)) |
| 115 | +>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 44, 13)) |
| 116 | + |
| 117 | + x; // {} |
| 118 | +>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 44, 13)) |
| 119 | + } |
| 120 | + else { |
| 121 | + x; // Record<string, unknown> |
| 122 | +>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 44, 13)) |
| 123 | + } |
| 124 | + x; // Record<string, unknown> |
| 125 | +>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 44, 13)) |
| 126 | +} |
| 127 | + |
| 128 | +// Repro from #50916 |
| 129 | + |
| 130 | +type Identity<T> = {[K in keyof T]: T[K]}; |
| 131 | +>Identity : Symbol(Identity, Decl(narrowingMutualSubtypes.ts, 52, 1)) |
| 132 | +>T : Symbol(T, Decl(narrowingMutualSubtypes.ts, 56, 14)) |
| 133 | +>K : Symbol(K, Decl(narrowingMutualSubtypes.ts, 56, 21)) |
| 134 | +>T : Symbol(T, Decl(narrowingMutualSubtypes.ts, 56, 14)) |
| 135 | +>T : Symbol(T, Decl(narrowingMutualSubtypes.ts, 56, 14)) |
| 136 | +>K : Symbol(K, Decl(narrowingMutualSubtypes.ts, 56, 21)) |
| 137 | + |
| 138 | +type Self<T> = T extends unknown ? Identity<T> : never; |
| 139 | +>Self : Symbol(Self, Decl(narrowingMutualSubtypes.ts, 56, 42)) |
| 140 | +>T : Symbol(T, Decl(narrowingMutualSubtypes.ts, 58, 10)) |
| 141 | +>T : Symbol(T, Decl(narrowingMutualSubtypes.ts, 58, 10)) |
| 142 | +>Identity : Symbol(Identity, Decl(narrowingMutualSubtypes.ts, 52, 1)) |
| 143 | +>T : Symbol(T, Decl(narrowingMutualSubtypes.ts, 58, 10)) |
| 144 | + |
| 145 | +function is<T>(value: T): value is Self<T> { |
| 146 | +>is : Symbol(is, Decl(narrowingMutualSubtypes.ts, 58, 55)) |
| 147 | +>T : Symbol(T, Decl(narrowingMutualSubtypes.ts, 60, 12)) |
| 148 | +>value : Symbol(value, Decl(narrowingMutualSubtypes.ts, 60, 15)) |
| 149 | +>T : Symbol(T, Decl(narrowingMutualSubtypes.ts, 60, 12)) |
| 150 | +>value : Symbol(value, Decl(narrowingMutualSubtypes.ts, 60, 15)) |
| 151 | +>Self : Symbol(Self, Decl(narrowingMutualSubtypes.ts, 56, 42)) |
| 152 | +>T : Symbol(T, Decl(narrowingMutualSubtypes.ts, 60, 12)) |
| 153 | + |
| 154 | + return true; |
| 155 | +} |
| 156 | + |
| 157 | +type Union = {a: number} | {b: number} | {c: number}; |
| 158 | +>Union : Symbol(Union, Decl(narrowingMutualSubtypes.ts, 62, 1)) |
| 159 | +>a : Symbol(a, Decl(narrowingMutualSubtypes.ts, 64, 15)) |
| 160 | +>b : Symbol(b, Decl(narrowingMutualSubtypes.ts, 64, 29)) |
| 161 | +>c : Symbol(c, Decl(narrowingMutualSubtypes.ts, 64, 43)) |
| 162 | + |
| 163 | +function example(x: Union) { |
| 164 | +>example : Symbol(example, Decl(narrowingMutualSubtypes.ts, 64, 54)) |
| 165 | +>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 66, 17)) |
| 166 | +>Union : Symbol(Union, Decl(narrowingMutualSubtypes.ts, 62, 1)) |
| 167 | + |
| 168 | + if (is(x)) {} |
| 169 | +>is : Symbol(is, Decl(narrowingMutualSubtypes.ts, 58, 55)) |
| 170 | +>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 66, 17)) |
| 171 | + |
| 172 | + if (is(x)) {} |
| 173 | +>is : Symbol(is, Decl(narrowingMutualSubtypes.ts, 58, 55)) |
| 174 | +>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 66, 17)) |
| 175 | + |
| 176 | + if (is(x)) {} |
| 177 | +>is : Symbol(is, Decl(narrowingMutualSubtypes.ts, 58, 55)) |
| 178 | +>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 66, 17)) |
| 179 | + |
| 180 | + if (is(x)) {} |
| 181 | +>is : Symbol(is, Decl(narrowingMutualSubtypes.ts, 58, 55)) |
| 182 | +>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 66, 17)) |
| 183 | + |
| 184 | + if (is(x)) {} |
| 185 | +>is : Symbol(is, Decl(narrowingMutualSubtypes.ts, 58, 55)) |
| 186 | +>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 66, 17)) |
| 187 | + |
| 188 | + if (is(x)) {} |
| 189 | +>is : Symbol(is, Decl(narrowingMutualSubtypes.ts, 58, 55)) |
| 190 | +>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 66, 17)) |
| 191 | + |
| 192 | + if (is(x)) {} |
| 193 | +>is : Symbol(is, Decl(narrowingMutualSubtypes.ts, 58, 55)) |
| 194 | +>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 66, 17)) |
| 195 | + |
| 196 | + if (is(x)) {} |
| 197 | +>is : Symbol(is, Decl(narrowingMutualSubtypes.ts, 58, 55)) |
| 198 | +>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 66, 17)) |
| 199 | + |
| 200 | + x; // Union |
| 201 | +>x : Symbol(x, Decl(narrowingMutualSubtypes.ts, 66, 17)) |
| 202 | +} |
| 203 | + |
0 commit comments