本文介绍了TypeScript的并集和交集类型的命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我无法理解TypeScript中的 union类型和交叉点类型背后的逻辑.I can't understand the logic behind the terms union types and intersection types in TypeScript.实用上,如果不同类型的属性是属性集,那么如果将它们与&运算符组合,则结果类型将是这些集合中的 union .按照这种逻辑,我希望这样的类型称为 union types .如果将它们与|结合使用,则只能使用它们的公共属性,即集合的 intersection .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. 维基百科似乎支持这种逻辑:但是,根据 typescriptlang.org ,恰好相反:&用于生成交叉点类型,而|用于生成工会类型.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.如果相交所有红色事物和所有小事物的集合,则最终会得到属性的 union -集合中的所有事物都具有红色财产和小财产.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.但是,如果您使用了红色小物件和蓝色小物件的 union ,则只有smallness属性在结果集中是通用的. 与相交红色小"与蓝色小"会产生小".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.以图像形式: 这篇关于TypeScript的并集和交集类型的命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-27 16:40