Closed
Description
TypeScript Version: 2.5.1-insiders.20170825
Code
interface A {
identity: 'a';
a: number;
}
interface B {
identity: 'b';
b: number;
}
interface C {}
// This function errors as written. It does not error if changed to `value: C & (A | B)`.
function f(value: ((A | B) & C) & (A | B)): number {
switch (value.identity) {
case 'a':
return value.a; // Error: Property 'a' does not exist on 'B & C'.
case 'b':
return value.b; // Error: Property 'b' does not exist on 'A & C'.
}
}
Expected behavior:
No error. This is a discriminated union and the switch statement should infer property a
.
Furthermore, I expect that the following types are equivalent:
((A | B) & C) & (A | B)
(A | B) & C & (A | B)
C & (A | B)
However, 1 behaves differently from 2 and 3.
Actual behavior:
TS errors because it fails to infer that the type cannot be B & C
in the first case.