I am trying to wrap my head around Union and Intersection types in TypeScript, and I've come across a case that's puzzling me. You can check it out on this Playground Link
interface A {
a: number;
}
interface B{
b: boolean;
}
type UnionCombinedType = A | B;
type IntersectionType = A & B;
const obj: UnionCombinedType = {
a: 6,
b: true,
}
const obj2: IntersectionType = {
a: 6,
b: true,
}
I'm confused about why I can assign values to both properties in the intersection type. Logically, the intersection between the two interfaces should be empty. If I interpret the &
as an AND
, then it makes sense why I can include both properties. However, if I treat the |
keyword as an OR
, I would expect to only be able to assign either a
or b
, not both.
Could someone provide some insight into these types?