type Union<A, B> = A extends B ? '1' : '2' type A = 1 | 2 type B = 1 type C = Union<A, B> // '1' | '2' ---为什么不是 '2'? type D = Union<B, A> // '1' type E = A extends B ? '1' : '2' // '2' type F = B extends A ? '1' : '2' // '1' 
type Union<A, B> = A extends B ? '1' : '2' type A = 1 | 2 type B = 1 type C = Union<A, B> // '1' | '2' ---为什么不是 '2'? type D = Union<B, A> // '1' type E = A extends B ? '1' : '2' // '2' type F = B extends A ? '1' : '2' // '1' 1 coolzjy Dec 15, 2022 ```ts 1 | 2 extends 2 ? '1' : '2' // 等价于 (1 extends 2 ? '1' : '2') | (2 extends 2 ? '1' : '2') ``` |
3 zhouyg Dec 15, 2022 X | Y 不仅代表了自身的一种类型描述,还涉及计算过程时还有多参数的含义 |
4 zhy0216 Dec 15, 2022 你把 | 理解成并集 & 理解成交集 extends 理解成 isSubSet 就很好理解了 这个资料 https://docs.google.com/presentation/d/1HYknblfcAnbjbdDe4MYTyRQpCw_gV72n2cj88xH7z8c/edit#slide=id.p |
5 rozbo Dec 15, 2022 你把他这个类型理解为给编译器看的就行,实际运行过程中类型不体现 |
9 dk7952638 Dec 16, 2022 ts 困惑类型就对了,这说明你入门了! |
11 xiaohundun Dec 16, 2022 联合类型的每一个 item 都参与运算,是这么解释的么 |