|
| 1 | +//// [dependentDestructuredVariables.ts] |
| 2 | +type Action = |
| 3 | + | { kind: 'A', payload: number } |
| 4 | + | { kind: 'B', payload: string }; |
| 5 | + |
| 6 | +function f10({ kind, payload }: Action) { |
| 7 | + if (kind === 'A') { |
| 8 | + payload.toFixed(); |
| 9 | + } |
| 10 | + if (kind === 'B') { |
| 11 | + payload.toUpperCase(); |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +function f11(action: Action) { |
| 16 | + const { kind, payload } = action; |
| 17 | + if (kind === 'A') { |
| 18 | + payload.toFixed(); |
| 19 | + } |
| 20 | + if (kind === 'B') { |
| 21 | + payload.toUpperCase(); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +function f12({ kind, payload }: Action) { |
| 26 | + switch (kind) { |
| 27 | + case 'A': |
| 28 | + payload.toFixed(); |
| 29 | + break; |
| 30 | + case 'B': |
| 31 | + payload.toUpperCase(); |
| 32 | + break; |
| 33 | + default: |
| 34 | + payload; // never |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +type Action2 = |
| 39 | + | { kind: 'A', payload: number | undefined } |
| 40 | + | { kind: 'B', payload: string | undefined }; |
| 41 | + |
| 42 | +function f20({ kind, payload }: Action2) { |
| 43 | + if (payload) { |
| 44 | + if (kind === 'A') { |
| 45 | + payload.toFixed(); |
| 46 | + } |
| 47 | + if (kind === 'B') { |
| 48 | + payload.toUpperCase(); |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +function f21(action: Action2) { |
| 54 | + const { kind, payload } = action; |
| 55 | + if (payload) { |
| 56 | + if (kind === 'A') { |
| 57 | + payload.toFixed(); |
| 58 | + } |
| 59 | + if (kind === 'B') { |
| 60 | + payload.toUpperCase(); |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +function f22(action: Action2) { |
| 66 | + if (action.payload) { |
| 67 | + const { kind, payload } = action; |
| 68 | + if (kind === 'A') { |
| 69 | + payload.toFixed(); |
| 70 | + } |
| 71 | + if (kind === 'B') { |
| 72 | + payload.toUpperCase(); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +function f23({ kind, payload }: Action2) { |
| 78 | + if (payload) { |
| 79 | + switch (kind) { |
| 80 | + case 'A': |
| 81 | + payload.toFixed(); |
| 82 | + break; |
| 83 | + case 'B': |
| 84 | + payload.toUpperCase(); |
| 85 | + break; |
| 86 | + default: |
| 87 | + payload; // never |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +type Foo = |
| 93 | + | { kind: 'A', isA: true } |
| 94 | + | { kind: 'B', isA: false } |
| 95 | + | { kind: 'C', isA: false }; |
| 96 | + |
| 97 | +function f30({ kind, isA }: Foo) { |
| 98 | + if (kind === 'A') { |
| 99 | + isA; // true |
| 100 | + } |
| 101 | + if (kind === 'B') { |
| 102 | + isA; // false |
| 103 | + } |
| 104 | + if (kind === 'C') { |
| 105 | + isA; // false |
| 106 | + } |
| 107 | + if (isA) { |
| 108 | + kind; // 'A' |
| 109 | + } |
| 110 | + else { |
| 111 | + kind; // 'B' | 'C' |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +// Repro from #35283 |
| 116 | + |
| 117 | +interface A<T> { variant: 'a', value: T } |
| 118 | + |
| 119 | +interface B<T> { variant: 'b', value: Array<T> } |
| 120 | + |
| 121 | +type AB<T> = A<T> | B<T>; |
| 122 | + |
| 123 | +declare function printValue<T>(t: T): void; |
| 124 | + |
| 125 | +declare function printValueList<T>(t: Array<T>): void; |
| 126 | + |
| 127 | +function unrefined1<T>(ab: AB<T>): void { |
| 128 | + const { variant, value } = ab; |
| 129 | + if (variant === 'a') { |
| 130 | + printValue<T>(value); |
| 131 | + } |
| 132 | + else { |
| 133 | + printValueList<T>(value); |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +// Repro from #38020 |
| 138 | + |
| 139 | +type Action3 = |
| 140 | + | {type: 'add', payload: { toAdd: number } } |
| 141 | + | {type: 'remove', payload: { toRemove: number } }; |
| 142 | + |
| 143 | +const reducerBroken = (state: number, { type, payload }: Action3) => { |
| 144 | + switch (type) { |
| 145 | + case 'add': |
| 146 | + return state + payload.toAdd; |
| 147 | + case 'remove': |
| 148 | + return state - payload.toRemove; |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +// Repro from #46143 |
| 153 | + |
| 154 | +declare var it: Iterator<number>; |
| 155 | +const { value, done } = it.next(); |
| 156 | +if (!done) { |
| 157 | + value; // number |
| 158 | +} |
| 159 | + |
| 160 | + |
| 161 | +//// [dependentDestructuredVariables.js] |
| 162 | +"use strict"; |
| 163 | +function f10({ kind, payload }) { |
| 164 | + if (kind === 'A') { |
| 165 | + payload.toFixed(); |
| 166 | + } |
| 167 | + if (kind === 'B') { |
| 168 | + payload.toUpperCase(); |
| 169 | + } |
| 170 | +} |
| 171 | +function f11(action) { |
| 172 | + const { kind, payload } = action; |
| 173 | + if (kind === 'A') { |
| 174 | + payload.toFixed(); |
| 175 | + } |
| 176 | + if (kind === 'B') { |
| 177 | + payload.toUpperCase(); |
| 178 | + } |
| 179 | +} |
| 180 | +function f12({ kind, payload }) { |
| 181 | + switch (kind) { |
| 182 | + case 'A': |
| 183 | + payload.toFixed(); |
| 184 | + break; |
| 185 | + case 'B': |
| 186 | + payload.toUpperCase(); |
| 187 | + break; |
| 188 | + default: |
| 189 | + payload; // never |
| 190 | + } |
| 191 | +} |
| 192 | +function f20({ kind, payload }) { |
| 193 | + if (payload) { |
| 194 | + if (kind === 'A') { |
| 195 | + payload.toFixed(); |
| 196 | + } |
| 197 | + if (kind === 'B') { |
| 198 | + payload.toUpperCase(); |
| 199 | + } |
| 200 | + } |
| 201 | +} |
| 202 | +function f21(action) { |
| 203 | + const { kind, payload } = action; |
| 204 | + if (payload) { |
| 205 | + if (kind === 'A') { |
| 206 | + payload.toFixed(); |
| 207 | + } |
| 208 | + if (kind === 'B') { |
| 209 | + payload.toUpperCase(); |
| 210 | + } |
| 211 | + } |
| 212 | +} |
| 213 | +function f22(action) { |
| 214 | + if (action.payload) { |
| 215 | + const { kind, payload } = action; |
| 216 | + if (kind === 'A') { |
| 217 | + payload.toFixed(); |
| 218 | + } |
| 219 | + if (kind === 'B') { |
| 220 | + payload.toUpperCase(); |
| 221 | + } |
| 222 | + } |
| 223 | +} |
| 224 | +function f23({ kind, payload }) { |
| 225 | + if (payload) { |
| 226 | + switch (kind) { |
| 227 | + case 'A': |
| 228 | + payload.toFixed(); |
| 229 | + break; |
| 230 | + case 'B': |
| 231 | + payload.toUpperCase(); |
| 232 | + break; |
| 233 | + default: |
| 234 | + payload; // never |
| 235 | + } |
| 236 | + } |
| 237 | +} |
| 238 | +function f30({ kind, isA }) { |
| 239 | + if (kind === 'A') { |
| 240 | + isA; // true |
| 241 | + } |
| 242 | + if (kind === 'B') { |
| 243 | + isA; // false |
| 244 | + } |
| 245 | + if (kind === 'C') { |
| 246 | + isA; // false |
| 247 | + } |
| 248 | + if (isA) { |
| 249 | + kind; // 'A' |
| 250 | + } |
| 251 | + else { |
| 252 | + kind; // 'B' | 'C' |
| 253 | + } |
| 254 | +} |
| 255 | +function unrefined1(ab) { |
| 256 | + const { variant, value } = ab; |
| 257 | + if (variant === 'a') { |
| 258 | + printValue(value); |
| 259 | + } |
| 260 | + else { |
| 261 | + printValueList(value); |
| 262 | + } |
| 263 | +} |
| 264 | +const reducerBroken = (state, { type, payload }) => { |
| 265 | + switch (type) { |
| 266 | + case 'add': |
| 267 | + return state + payload.toAdd; |
| 268 | + case 'remove': |
| 269 | + return state - payload.toRemove; |
| 270 | + } |
| 271 | +}; |
| 272 | +const { value, done } = it.next(); |
| 273 | +if (!done) { |
| 274 | + value; // number |
| 275 | +} |
| 276 | + |
| 277 | + |
| 278 | +//// [dependentDestructuredVariables.d.ts] |
| 279 | +declare type Action = { |
| 280 | + kind: 'A'; |
| 281 | + payload: number; |
| 282 | +} | { |
| 283 | + kind: 'B'; |
| 284 | + payload: string; |
| 285 | +}; |
| 286 | +declare function f10({ kind, payload }: Action): void; |
| 287 | +declare function f11(action: Action): void; |
| 288 | +declare function f12({ kind, payload }: Action): void; |
| 289 | +declare type Action2 = { |
| 290 | + kind: 'A'; |
| 291 | + payload: number | undefined; |
| 292 | +} | { |
| 293 | + kind: 'B'; |
| 294 | + payload: string | undefined; |
| 295 | +}; |
| 296 | +declare function f20({ kind, payload }: Action2): void; |
| 297 | +declare function f21(action: Action2): void; |
| 298 | +declare function f22(action: Action2): void; |
| 299 | +declare function f23({ kind, payload }: Action2): void; |
| 300 | +declare type Foo = { |
| 301 | + kind: 'A'; |
| 302 | + isA: true; |
| 303 | +} | { |
| 304 | + kind: 'B'; |
| 305 | + isA: false; |
| 306 | +} | { |
| 307 | + kind: 'C'; |
| 308 | + isA: false; |
| 309 | +}; |
| 310 | +declare function f30({ kind, isA }: Foo): void; |
| 311 | +interface A<T> { |
| 312 | + variant: 'a'; |
| 313 | + value: T; |
| 314 | +} |
| 315 | +interface B<T> { |
| 316 | + variant: 'b'; |
| 317 | + value: Array<T>; |
| 318 | +} |
| 319 | +declare type AB<T> = A<T> | B<T>; |
| 320 | +declare function printValue<T>(t: T): void; |
| 321 | +declare function printValueList<T>(t: Array<T>): void; |
| 322 | +declare function unrefined1<T>(ab: AB<T>): void; |
| 323 | +declare type Action3 = { |
| 324 | + type: 'add'; |
| 325 | + payload: { |
| 326 | + toAdd: number; |
| 327 | + }; |
| 328 | +} | { |
| 329 | + type: 'remove'; |
| 330 | + payload: { |
| 331 | + toRemove: number; |
| 332 | + }; |
| 333 | +}; |
| 334 | +declare const reducerBroken: (state: number, { type, payload }: Action3) => number; |
| 335 | +declare var it: Iterator<number>; |
| 336 | +declare const value: any, done: boolean | undefined; |
0 commit comments