簡體   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