繁体   English   中英

不同类型联合的交集不是我所期望的

[英]Intersection of union of different types is not what I expected

type T1 = 1 | 'b' & string | number // "b" | number
type T2 = 1 | 2 & 1 | 3 // 1 | 3

我是TS的新手。 谁能告诉我为什么是"b" | number "b" | number1 | 3 1 | 3这里发生了什么?

&的优先级高于|

因此,您的代码被解释为:

type T1 = 1 | ('b' & string) | number 
type T2 = 1 | (2 & 1) | 3

在这种情况下"b" | number "b" | number1 | 3 1 | 3有道理。

如果您添加括号,它将按照您的想法工作:

type T1 = (1 | 'b') & (string | number) // 1 | "b"
type T2 = (1 | 2) & (1 | 3) // 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM