I can't understand the logic behind the terms intersection types and union types in TypeScript. I'm thinking of interfaces (or classes) as sets of properties.
The logical conjunction operator &
is equivalent to intersection in set theory, defined as:
The intersection of two sets A and B, is the set containing all elements of A that also belong to B
In TypeScript, an Intersection Type is also built using the &
operator and is defined as:
The intersection type of interface
Colorful
and interfaceCircle
is a new type that has all the members ofColorful
andCircle
This is the exact opposite of how intersection is defined in mathematics and set theory.
I'm sure there is another way of looking at it, but I cannot figure it out.
T | U
ismembers(T) | members(U)
and similarly members ofT & U
are members of bothT
andU
so are in the intersection ofmembers(T)
andmembers(U)
. – Orlosky∨ := ∪ (union)
,v
meansor
, so union isor
, which is represented with|
in typescript. – Herat