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