|
| 1 | +=== tests/cases/compiler/narrowingTypeofDiscriminant.ts === |
| 2 | +function f1(obj: { kind: 'a', data: string } | { kind: 1, data: number }) { |
| 3 | +>f1 : Symbol(f1, Decl(narrowingTypeofDiscriminant.ts, 0, 0)) |
| 4 | +>obj : Symbol(obj, Decl(narrowingTypeofDiscriminant.ts, 0, 12)) |
| 5 | +>kind : Symbol(kind, Decl(narrowingTypeofDiscriminant.ts, 0, 18)) |
| 6 | +>data : Symbol(data, Decl(narrowingTypeofDiscriminant.ts, 0, 29)) |
| 7 | +>kind : Symbol(kind, Decl(narrowingTypeofDiscriminant.ts, 0, 48)) |
| 8 | +>data : Symbol(data, Decl(narrowingTypeofDiscriminant.ts, 0, 57)) |
| 9 | + |
| 10 | + if (typeof obj.kind === "string") { |
| 11 | +>obj.kind : Symbol(kind, Decl(narrowingTypeofDiscriminant.ts, 0, 18), Decl(narrowingTypeofDiscriminant.ts, 0, 48)) |
| 12 | +>obj : Symbol(obj, Decl(narrowingTypeofDiscriminant.ts, 0, 12)) |
| 13 | +>kind : Symbol(kind, Decl(narrowingTypeofDiscriminant.ts, 0, 18), Decl(narrowingTypeofDiscriminant.ts, 0, 48)) |
| 14 | + |
| 15 | + obj; // { kind: 'a', data: string } |
| 16 | +>obj : Symbol(obj, Decl(narrowingTypeofDiscriminant.ts, 0, 12)) |
| 17 | + } |
| 18 | + else { |
| 19 | + obj; // { kind: 1, data: number } |
| 20 | +>obj : Symbol(obj, Decl(narrowingTypeofDiscriminant.ts, 0, 12)) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +function f2(obj: { kind: 'a', data: string } | { kind: 1, data: number } | undefined) { |
| 25 | +>f2 : Symbol(f2, Decl(narrowingTypeofDiscriminant.ts, 7, 1)) |
| 26 | +>obj : Symbol(obj, Decl(narrowingTypeofDiscriminant.ts, 9, 12)) |
| 27 | +>kind : Symbol(kind, Decl(narrowingTypeofDiscriminant.ts, 9, 18)) |
| 28 | +>data : Symbol(data, Decl(narrowingTypeofDiscriminant.ts, 9, 29)) |
| 29 | +>kind : Symbol(kind, Decl(narrowingTypeofDiscriminant.ts, 9, 48)) |
| 30 | +>data : Symbol(data, Decl(narrowingTypeofDiscriminant.ts, 9, 57)) |
| 31 | + |
| 32 | + if (typeof obj?.kind === "string") { |
| 33 | +>obj?.kind : Symbol(kind, Decl(narrowingTypeofDiscriminant.ts, 9, 18), Decl(narrowingTypeofDiscriminant.ts, 9, 48)) |
| 34 | +>obj : Symbol(obj, Decl(narrowingTypeofDiscriminant.ts, 9, 12)) |
| 35 | +>kind : Symbol(kind, Decl(narrowingTypeofDiscriminant.ts, 9, 18), Decl(narrowingTypeofDiscriminant.ts, 9, 48)) |
| 36 | + |
| 37 | + obj; // { kind: 'a', data: string } |
| 38 | +>obj : Symbol(obj, Decl(narrowingTypeofDiscriminant.ts, 9, 12)) |
| 39 | + } |
| 40 | + else { |
| 41 | + obj; // { kind: 1, data: number } | undefined |
| 42 | +>obj : Symbol(obj, Decl(narrowingTypeofDiscriminant.ts, 9, 12)) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// Repro from #51700 |
| 47 | + |
| 48 | +type WrappedStringOr<T> = { value?: string } | { value?: T }; |
| 49 | +>WrappedStringOr : Symbol(WrappedStringOr, Decl(narrowingTypeofDiscriminant.ts, 16, 1)) |
| 50 | +>T : Symbol(T, Decl(narrowingTypeofDiscriminant.ts, 20, 21)) |
| 51 | +>value : Symbol(value, Decl(narrowingTypeofDiscriminant.ts, 20, 27)) |
| 52 | +>value : Symbol(value, Decl(narrowingTypeofDiscriminant.ts, 20, 48)) |
| 53 | +>T : Symbol(T, Decl(narrowingTypeofDiscriminant.ts, 20, 21)) |
| 54 | + |
| 55 | +function numberOk(wrapped: WrappedStringOr<number> | null) { |
| 56 | +>numberOk : Symbol(numberOk, Decl(narrowingTypeofDiscriminant.ts, 20, 61)) |
| 57 | +>wrapped : Symbol(wrapped, Decl(narrowingTypeofDiscriminant.ts, 22, 18)) |
| 58 | +>WrappedStringOr : Symbol(WrappedStringOr, Decl(narrowingTypeofDiscriminant.ts, 16, 1)) |
| 59 | + |
| 60 | + if (typeof wrapped?.value !== 'string') { |
| 61 | +>wrapped?.value : Symbol(value, Decl(narrowingTypeofDiscriminant.ts, 20, 27), Decl(narrowingTypeofDiscriminant.ts, 20, 48)) |
| 62 | +>wrapped : Symbol(wrapped, Decl(narrowingTypeofDiscriminant.ts, 22, 18)) |
| 63 | +>value : Symbol(value, Decl(narrowingTypeofDiscriminant.ts, 20, 27), Decl(narrowingTypeofDiscriminant.ts, 20, 48)) |
| 64 | + |
| 65 | + return null; |
| 66 | + } |
| 67 | + return wrapped.value; |
| 68 | +>wrapped.value : Symbol(value, Decl(narrowingTypeofDiscriminant.ts, 20, 27), Decl(narrowingTypeofDiscriminant.ts, 20, 48)) |
| 69 | +>wrapped : Symbol(wrapped, Decl(narrowingTypeofDiscriminant.ts, 22, 18)) |
| 70 | +>value : Symbol(value, Decl(narrowingTypeofDiscriminant.ts, 20, 27), Decl(narrowingTypeofDiscriminant.ts, 20, 48)) |
| 71 | +} |
| 72 | + |
| 73 | +function booleanBad(wrapped: WrappedStringOr<boolean> | null) { |
| 74 | +>booleanBad : Symbol(booleanBad, Decl(narrowingTypeofDiscriminant.ts, 27, 1)) |
| 75 | +>wrapped : Symbol(wrapped, Decl(narrowingTypeofDiscriminant.ts, 29, 20)) |
| 76 | +>WrappedStringOr : Symbol(WrappedStringOr, Decl(narrowingTypeofDiscriminant.ts, 16, 1)) |
| 77 | + |
| 78 | + if (typeof wrapped?.value !== 'string') { |
| 79 | +>wrapped?.value : Symbol(value, Decl(narrowingTypeofDiscriminant.ts, 20, 27), Decl(narrowingTypeofDiscriminant.ts, 20, 48)) |
| 80 | +>wrapped : Symbol(wrapped, Decl(narrowingTypeofDiscriminant.ts, 29, 20)) |
| 81 | +>value : Symbol(value, Decl(narrowingTypeofDiscriminant.ts, 20, 27), Decl(narrowingTypeofDiscriminant.ts, 20, 48)) |
| 82 | + |
| 83 | + return null; |
| 84 | + } |
| 85 | + return wrapped.value; |
| 86 | +>wrapped.value : Symbol(value, Decl(narrowingTypeofDiscriminant.ts, 20, 27)) |
| 87 | +>wrapped : Symbol(wrapped, Decl(narrowingTypeofDiscriminant.ts, 29, 20)) |
| 88 | +>value : Symbol(value, Decl(narrowingTypeofDiscriminant.ts, 20, 27)) |
| 89 | +} |
| 90 | + |
| 91 | +function booleanFixed(wrapped: WrappedStringOr<boolean> | null) { |
| 92 | +>booleanFixed : Symbol(booleanFixed, Decl(narrowingTypeofDiscriminant.ts, 34, 1)) |
| 93 | +>wrapped : Symbol(wrapped, Decl(narrowingTypeofDiscriminant.ts, 36, 22)) |
| 94 | +>WrappedStringOr : Symbol(WrappedStringOr, Decl(narrowingTypeofDiscriminant.ts, 16, 1)) |
| 95 | + |
| 96 | + if (typeof (wrapped?.value) !== 'string') { |
| 97 | +>wrapped?.value : Symbol(value, Decl(narrowingTypeofDiscriminant.ts, 20, 27), Decl(narrowingTypeofDiscriminant.ts, 20, 48)) |
| 98 | +>wrapped : Symbol(wrapped, Decl(narrowingTypeofDiscriminant.ts, 36, 22)) |
| 99 | +>value : Symbol(value, Decl(narrowingTypeofDiscriminant.ts, 20, 27), Decl(narrowingTypeofDiscriminant.ts, 20, 48)) |
| 100 | + |
| 101 | + return null; |
| 102 | + } |
| 103 | + return wrapped.value; |
| 104 | +>wrapped.value : Symbol(value, Decl(narrowingTypeofDiscriminant.ts, 20, 27)) |
| 105 | +>wrapped : Symbol(wrapped, Decl(narrowingTypeofDiscriminant.ts, 36, 22)) |
| 106 | +>value : Symbol(value, Decl(narrowingTypeofDiscriminant.ts, 20, 27)) |
| 107 | +} |
| 108 | + |
0 commit comments