TypeScript 的并集和交集类型的命名 [英] Naming of TypeScript's union and intersection types

查看:23
本文介绍了TypeScript 的并集和交集类型的命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解 TypeScript 中联合类型交集类型这两个术语背后的逻辑.

实际上,如果不同类型的属性是一组属性,如果我将它们与 & 运算符组合,则结果类型将是那些套按照这个逻辑,我希望这样的类型被称为联合类型.如果我将它们与 | 结合起来,我只能使用它们的公共属性,即集合的交集.

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.

解决方案

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:

这篇关于TypeScript 的并集和交集类型的命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆