Closed
Description
TypeScript Version: 2.4.0
Code
type A = {
foo?: {
bar?: string;
}
}
type B = {
foo?: {
baz?: number
};
}
type C = A & B;
let c: C = { foo: { bar: '123', baz: 123 } };
if (c.foo) {
console.log(c.foo.bar); // throws error
}
Expected behavior:
This should not raise an error, as I've already checked for existance of foo
in c
, so the object in foo
should have both bar
and baz
as optional with their respective types. The type should be refined because I've already checked for the existance of foo
.
Actual behavior:
Typescript shows an error when trying to access either bar
or baz
, as each property doesn't exist in one of the combinations of the intersection type, but the combination includes foo
being undefined
, which I checked above.