|
| 1 | +//// [tests/cases/compiler/discriminantOrderIndependence.ts] //// |
| 2 | + |
| 3 | +=== discriminantOrderIndependence.ts === |
| 4 | +interface A { |
| 5 | +>A : Symbol(A, Decl(discriminantOrderIndependence.ts, 0, 0)) |
| 6 | + |
| 7 | + subType: "b"; |
| 8 | +>subType : Symbol(A.subType, Decl(discriminantOrderIndependence.ts, 0, 13)) |
| 9 | + |
| 10 | + type: "a"; |
| 11 | +>type : Symbol(A.type, Decl(discriminantOrderIndependence.ts, 1, 17)) |
| 12 | +} |
| 13 | + |
| 14 | +declare let order1: |
| 15 | +>order1 : Symbol(order1, Decl(discriminantOrderIndependence.ts, 5, 11)) |
| 16 | + |
| 17 | + | { type: "1" } |
| 18 | +>type : Symbol(type, Decl(discriminantOrderIndependence.ts, 6, 7)) |
| 19 | + |
| 20 | + | A |
| 21 | +>A : Symbol(A, Decl(discriminantOrderIndependence.ts, 0, 0)) |
| 22 | + |
| 23 | + | { type: "2" } |
| 24 | +>type : Symbol(type, Decl(discriminantOrderIndependence.ts, 8, 7)) |
| 25 | + |
| 26 | + | { type: "3" } |
| 27 | +>type : Symbol(type, Decl(discriminantOrderIndependence.ts, 9, 7)) |
| 28 | + |
| 29 | + | undefined; |
| 30 | + |
| 31 | +// Should NOT error: 'order1' is possibly 'undefined' after the guard |
| 32 | +if (order1 && order1.type === "a") { |
| 33 | +>order1 : Symbol(order1, Decl(discriminantOrderIndependence.ts, 5, 11)) |
| 34 | +>order1.type : Symbol(type, Decl(discriminantOrderIndependence.ts, 1, 17), Decl(discriminantOrderIndependence.ts, 6, 7), Decl(discriminantOrderIndependence.ts, 8, 7), Decl(discriminantOrderIndependence.ts, 9, 7)) |
| 35 | +>order1 : Symbol(order1, Decl(discriminantOrderIndependence.ts, 5, 11)) |
| 36 | +>type : Symbol(type, Decl(discriminantOrderIndependence.ts, 1, 17), Decl(discriminantOrderIndependence.ts, 6, 7), Decl(discriminantOrderIndependence.ts, 8, 7), Decl(discriminantOrderIndependence.ts, 9, 7)) |
| 37 | + |
| 38 | + order1.type; // Should be OK |
| 39 | +>order1.type : Symbol(A.type, Decl(discriminantOrderIndependence.ts, 1, 17)) |
| 40 | +>order1 : Symbol(order1, Decl(discriminantOrderIndependence.ts, 5, 11)) |
| 41 | +>type : Symbol(A.type, Decl(discriminantOrderIndependence.ts, 1, 17)) |
| 42 | +} |
| 43 | + |
| 44 | +interface B { |
| 45 | +>B : Symbol(B, Decl(discriminantOrderIndependence.ts, 15, 1)) |
| 46 | + |
| 47 | + subType: "b"; |
| 48 | +>subType : Symbol(B.subType, Decl(discriminantOrderIndependence.ts, 17, 13)) |
| 49 | + |
| 50 | + type: "a"; |
| 51 | +>type : Symbol(B.type, Decl(discriminantOrderIndependence.ts, 18, 17)) |
| 52 | +} |
| 53 | + |
| 54 | +declare let order2: |
| 55 | +>order2 : Symbol(order2, Decl(discriminantOrderIndependence.ts, 22, 11)) |
| 56 | + |
| 57 | + | { type: "1" } |
| 58 | +>type : Symbol(type, Decl(discriminantOrderIndependence.ts, 23, 7)) |
| 59 | + |
| 60 | + | { type: "2" } |
| 61 | +>type : Symbol(type, Decl(discriminantOrderIndependence.ts, 24, 7)) |
| 62 | + |
| 63 | + | { type: "3" } |
| 64 | +>type : Symbol(type, Decl(discriminantOrderIndependence.ts, 25, 7)) |
| 65 | + |
| 66 | + | B |
| 67 | +>B : Symbol(B, Decl(discriminantOrderIndependence.ts, 15, 1)) |
| 68 | + |
| 69 | + | undefined; |
| 70 | + |
| 71 | +// Should NOT error: 'order2' is possibly 'undefined' after the guard |
| 72 | +if (order2 && order2.type === "a") { |
| 73 | +>order2 : Symbol(order2, Decl(discriminantOrderIndependence.ts, 22, 11)) |
| 74 | +>order2.type : Symbol(type, Decl(discriminantOrderIndependence.ts, 18, 17), Decl(discriminantOrderIndependence.ts, 23, 7), Decl(discriminantOrderIndependence.ts, 24, 7), Decl(discriminantOrderIndependence.ts, 25, 7)) |
| 75 | +>order2 : Symbol(order2, Decl(discriminantOrderIndependence.ts, 22, 11)) |
| 76 | +>type : Symbol(type, Decl(discriminantOrderIndependence.ts, 18, 17), Decl(discriminantOrderIndependence.ts, 23, 7), Decl(discriminantOrderIndependence.ts, 24, 7), Decl(discriminantOrderIndependence.ts, 25, 7)) |
| 77 | + |
| 78 | + order2.type; // Should be OK |
| 79 | +>order2.type : Symbol(B.type, Decl(discriminantOrderIndependence.ts, 18, 17)) |
| 80 | +>order2 : Symbol(order2, Decl(discriminantOrderIndependence.ts, 22, 11)) |
| 81 | +>type : Symbol(B.type, Decl(discriminantOrderIndependence.ts, 18, 17)) |
| 82 | +} |
| 83 | + |
| 84 | +// Also test with !. type assertion |
| 85 | +if (order1 && order1.type === "a") { |
| 86 | +>order1 : Symbol(order1, Decl(discriminantOrderIndependence.ts, 5, 11)) |
| 87 | +>order1.type : Symbol(type, Decl(discriminantOrderIndependence.ts, 1, 17), Decl(discriminantOrderIndependence.ts, 6, 7), Decl(discriminantOrderIndependence.ts, 8, 7), Decl(discriminantOrderIndependence.ts, 9, 7)) |
| 88 | +>order1 : Symbol(order1, Decl(discriminantOrderIndependence.ts, 5, 11)) |
| 89 | +>type : Symbol(type, Decl(discriminantOrderIndependence.ts, 1, 17), Decl(discriminantOrderIndependence.ts, 6, 7), Decl(discriminantOrderIndependence.ts, 8, 7), Decl(discriminantOrderIndependence.ts, 9, 7)) |
| 90 | + |
| 91 | + order1.type; // Should be OK |
| 92 | +>order1.type : Symbol(A.type, Decl(discriminantOrderIndependence.ts, 1, 17)) |
| 93 | +>order1 : Symbol(order1, Decl(discriminantOrderIndependence.ts, 5, 11)) |
| 94 | +>type : Symbol(A.type, Decl(discriminantOrderIndependence.ts, 1, 17)) |
| 95 | +} |
| 96 | +if (order2 && order2.type === "a") { |
| 97 | +>order2 : Symbol(order2, Decl(discriminantOrderIndependence.ts, 22, 11)) |
| 98 | +>order2.type : Symbol(type, Decl(discriminantOrderIndependence.ts, 18, 17), Decl(discriminantOrderIndependence.ts, 23, 7), Decl(discriminantOrderIndependence.ts, 24, 7), Decl(discriminantOrderIndependence.ts, 25, 7)) |
| 99 | +>order2 : Symbol(order2, Decl(discriminantOrderIndependence.ts, 22, 11)) |
| 100 | +>type : Symbol(type, Decl(discriminantOrderIndependence.ts, 18, 17), Decl(discriminantOrderIndependence.ts, 23, 7), Decl(discriminantOrderIndependence.ts, 24, 7), Decl(discriminantOrderIndependence.ts, 25, 7)) |
| 101 | + |
| 102 | + order2.type; // Should be OK |
| 103 | +>order2.type : Symbol(B.type, Decl(discriminantOrderIndependence.ts, 18, 17)) |
| 104 | +>order2 : Symbol(order2, Decl(discriminantOrderIndependence.ts, 22, 11)) |
| 105 | +>type : Symbol(B.type, Decl(discriminantOrderIndependence.ts, 18, 17)) |
| 106 | +} |
0 commit comments