From what I know of set theory, when you take a union of two sets, you overlay two sets and take the resulting set. When you take the intersection of two sets, you overlay two sets and take the parts that overlap with each other.
For numbers this works just fine:
type SomeUnion = (1 | 2 | 3) | (2 | 3 | 4)
const someUnion: SomeUnion // typeof someUnion is 1 | 2 | 3 | 4
type SomeIntersect = (1 | 2 | 3) & (2 | 3 | 4)
const someIntersect: SomeIntersect // typeof someIntersect is 2 | 3
For object keys the intersection and union operates work quite unintuitively in my opinion.
type ObjUnion = { one: string, two: string } | { two: string, three: string }
const objUnionKeys: keyof ObjUnion // typeof objUnionKeys is 'two' while I would expect it to be all keys -> 'one' | 'two' | 'three'
type ObjIntersection = { one: string, two: string } & { two: string, three: string }
const objIntersectionKeys: keyof ObjIntersection // typeof objIntersectionKeys is 'one' | 'two' | 'three' while I would expect it only to be the keys in common -> 'one'
I imagine that there is a solid reason for why it works like this. Can anyone fill me in?