[英]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" | number
和1 | 3
1 | 3
这里发生了什么?
&
的优先级高于|
因此,您的代码被解释为:
type T1 = 1 | ('b' & string) | number
type T2 = 1 | (2 & 1) | 3
在这种情况下"b" | number
"b" | number
和1 | 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.