I am trying to get an intuition about Union and Intersection types in typescript, but I can't figure out this case: 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,
}
Why am I allow to put both values in the intersection type? The intersection between the two interfaces is empty. If I read the &
as AND
then it's clear to me why it allows me to add both props, but then I should read the |
keyword as OR
and I would expect it to allow me to assign only a
or b
but not both.
Can someone give me some intuition about those types?
A & B
in your example can take b prop? it's not assignable to typeA
– Mussorgsky