|
| 1 | +=== tests/cases/compiler/narrowingIntersection.ts === |
| 2 | +// Somehow this being an intersection matters. |
| 3 | +type FooAndBaz = { foo: unknown } & { baz: unknown }; |
| 4 | +>FooAndBaz : Symbol(FooAndBaz, Decl(narrowingIntersection.ts, 0, 0)) |
| 5 | +>foo : Symbol(foo, Decl(narrowingIntersection.ts, 1, 18)) |
| 6 | +>baz : Symbol(baz, Decl(narrowingIntersection.ts, 1, 37)) |
| 7 | + |
| 8 | +type Disjoint = |
| 9 | +>Disjoint : Symbol(Disjoint, Decl(narrowingIntersection.ts, 1, 53)) |
| 10 | + |
| 11 | + | { readonly value: string; readonly err?: never; } |
| 12 | +>value : Symbol(value, Decl(narrowingIntersection.ts, 4, 4)) |
| 13 | +>err : Symbol(err, Decl(narrowingIntersection.ts, 4, 28)) |
| 14 | + |
| 15 | + | { readonly value?: never; readonly err: FooAndBaz; }; |
| 16 | +>value : Symbol(value, Decl(narrowingIntersection.ts, 5, 4)) |
| 17 | +>err : Symbol(err, Decl(narrowingIntersection.ts, 5, 28)) |
| 18 | +>FooAndBaz : Symbol(FooAndBaz, Decl(narrowingIntersection.ts, 0, 0)) |
| 19 | + |
| 20 | +function test1(result: Disjoint): string { |
| 21 | +>test1 : Symbol(test1, Decl(narrowingIntersection.ts, 5, 56)) |
| 22 | +>result : Symbol(result, Decl(narrowingIntersection.ts, 7, 15)) |
| 23 | +>Disjoint : Symbol(Disjoint, Decl(narrowingIntersection.ts, 1, 53)) |
| 24 | + |
| 25 | + if (result.err) { |
| 26 | +>result.err : Symbol(err, Decl(narrowingIntersection.ts, 4, 28), Decl(narrowingIntersection.ts, 5, 28)) |
| 27 | +>result : Symbol(result, Decl(narrowingIntersection.ts, 7, 15)) |
| 28 | +>err : Symbol(err, Decl(narrowingIntersection.ts, 4, 28), Decl(narrowingIntersection.ts, 5, 28)) |
| 29 | + |
| 30 | + throw result.err; |
| 31 | +>result.err : Symbol(err, Decl(narrowingIntersection.ts, 4, 28), Decl(narrowingIntersection.ts, 5, 28)) |
| 32 | +>result : Symbol(result, Decl(narrowingIntersection.ts, 7, 15)) |
| 33 | +>err : Symbol(err, Decl(narrowingIntersection.ts, 4, 28), Decl(narrowingIntersection.ts, 5, 28)) |
| 34 | + } |
| 35 | + // Error, should OK |
| 36 | + return result.value; |
| 37 | +>result.value : Symbol(value, Decl(narrowingIntersection.ts, 4, 4), Decl(narrowingIntersection.ts, 5, 4)) |
| 38 | +>result : Symbol(result, Decl(narrowingIntersection.ts, 7, 15)) |
| 39 | +>value : Symbol(value, Decl(narrowingIntersection.ts, 4, 4), Decl(narrowingIntersection.ts, 5, 4)) |
| 40 | +} |
| 41 | + |
| 42 | +type TrivialIntersection = { a: 1 } & { a: 1 }; |
| 43 | +>TrivialIntersection : Symbol(TrivialIntersection, Decl(narrowingIntersection.ts, 13, 1)) |
| 44 | +>a : Symbol(a, Decl(narrowingIntersection.ts, 15, 28)) |
| 45 | +>a : Symbol(a, Decl(narrowingIntersection.ts, 15, 39)) |
| 46 | + |
| 47 | +function want0(x: 0) {} |
| 48 | +>want0 : Symbol(want0, Decl(narrowingIntersection.ts, 15, 47)) |
| 49 | +>x : Symbol(x, Decl(narrowingIntersection.ts, 17, 15)) |
| 50 | + |
| 51 | +function test2(a: 0 | TrivialIntersection) { |
| 52 | +>test2 : Symbol(test2, Decl(narrowingIntersection.ts, 17, 23)) |
| 53 | +>a : Symbol(a, Decl(narrowingIntersection.ts, 19, 15)) |
| 54 | +>TrivialIntersection : Symbol(TrivialIntersection, Decl(narrowingIntersection.ts, 13, 1)) |
| 55 | + |
| 56 | + if (a === 0) { |
| 57 | +>a : Symbol(a, Decl(narrowingIntersection.ts, 19, 15)) |
| 58 | + |
| 59 | + want0(a); // Fails, but expect to work |
| 60 | +>want0 : Symbol(want0, Decl(narrowingIntersection.ts, 15, 47)) |
| 61 | +>a : Symbol(a, Decl(narrowingIntersection.ts, 19, 15)) |
| 62 | + } |
| 63 | +} |
0 commit comments