-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
645 - Diff #3014
Comments
很欣赏你的解释!😁 |
do it without Omit // O&O1 - O|O1
// & means 'either has', | means 'both have'
type Diff<O, O1> = {
[K in keyof (O & O1) as K extends keyof (O | O1) ? never : K]: (O & O1)[K];
}; |
这部分我有些疑惑,似乎在使用联合类型 Foo | Bar 的时候,既可以是两者的交集也可以是两者的并集
|
@L1atte 你这只是普通的联合类型使用吧,m 匹配到的是 Foo,n 匹配到的是 Bar,和交集并集没啥关系吧 |
666 |
type T1=Foo&Bar; |
why below code does not pass testcases? |
use '&" replace the '|',and wrap the expression with Omit<exp,never>,such like this: |
type Diff<O, O1, K1 extends keyof O = keyof O> = Omit<O & O1, K1 extends keyof O & keyof O1 ? K1 : never> |
@shun-quan
type Diff<O, O1> = Omit<O & O1, keyof O & keyof O1 >
or
type Diff<O, O1> = Omit<O & O1, keyof (O | O1) > |
@DoubleWoodLin |
the keypoint of this question is to understand the differences: type Result = keyof (Foo | Bar) // 'name' | 'age' |
all the answer not consider the same key, but key type difference,
|
解释: 在对象中使用交、并集
在对象中使用
|
与&
,与在非对象中使用存在语义上的差异。在集合对象中使用联合类型
|
,官网 working-with-union-types 有如下说明:在集合对象中使用交集类型
&
,可以见 intersection-types 给出的 demo:结合
&
与|
的使用,我们能立马写出比如类型diff
The text was updated successfully, but these errors were encountered: