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.
TypeScript Version: 2.4.0
Code
Expected behavior:
This should not raise an error, as I've already checked for existance of
fooinc, so the object infooshould have bothbarandbazas optional with their respective types. The type should be refined because I've already checked for the existance offoo.Actual behavior:
Typescript shows an error when trying to access either
barorbaz, as each property doesn't exist in one of the combinations of the intersection type, but the combination includesfoobeingundefined, which I checked above.