Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
700 views
in Technique[技术] by (71.8m points)

set - Naming of TypeScript's union and intersection types

I can't understand the logic behind the terms union types and intersection types in TypeScript.

Pragmatically, if the properties of different types are sets of properties, if I combine them with the & operator, the resulting type will be the union of the of those sets. Following that logic, I would expect types like this to be called union types. If I combine them with |, I can only use the common properties of them, the intersection of the sets.

Wikipedia seems to back that logic:

The power set (set of all subsets) of any given nonempty set S forms a Boolean algebra, an algebra of sets, with the two operations ∨ := ∪ (union) and ∧ := ∩ (intersection).

However, according to typescriptlang.org, it's exactly the opposite: & is used to produce intersection types and | is used for union types.

I'm sure there is another way of looking at it, but I cannot figure it out.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's another way to think about it. Consider four sets: Red things, blue things, big things, and small things.

If you intersect the set of all red things and all small things, you end up with the union of the properties -- everything in the set has both the red property and the small property.

But if you took the union of red small things and blue small things, only the smallness property is universal in the resulting set. Intersecting "red small" with "blue small" produces "small".

In other words, taking the union of the domain of values produces an intersected set of properties, and vice versa.

In image form: enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...