跳到主要内容

Diff

介绍

获取两个接口类型中的差值属性。

ts
type Foo = {
a: string;
b: number;
}
type Bar = {
a: string;
c: boolean
}
type Result1 = Diff<Foo,Bar> // { b: number, c: boolean }
type Result2 = Diff<Bar,Foo> // { b: number, c: boolean }
ts
type Foo = {
a: string;
b: number;
}
type Bar = {
a: string;
c: boolean
}
type Result1 = Diff<Foo,Bar> // { b: number, c: boolean }
type Result2 = Diff<Bar,Foo> // { b: number, c: boolean }
View on GitHub

起点

ts
/* _____________ Your Code Here _____________ */
 
type Diff<O, O1> = any
 
/* _____________ Test Cases _____________ */
 
 
type Foo = {
name: string
age: string
}
type Bar = {
name: string
age: string
gender: number
}
type Coo = {
name: string
gender: number
}
 
type cases = [
Expect<Equal<Diff<Foo, Bar>, { gender: number }>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Diff<Bar, Foo>, { gender: number }>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Diff<Foo, Coo>, { age: string, gender: number }>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Diff<Coo, Foo>, { age: string, gender: number }>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
]
 
ts
/* _____________ Your Code Here _____________ */
 
type Diff<O, O1> = any
 
/* _____________ Test Cases _____________ */
 
 
type Foo = {
name: string
age: string
}
type Bar = {
name: string
age: string
gender: number
}
type Coo = {
name: string
gender: number
}
 
type cases = [
Expect<Equal<Diff<Foo, Bar>, { gender: number }>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Diff<Bar, Foo>, { gender: number }>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Diff<Foo, Coo>, { age: string, gender: number }>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
Expect<Equal<Diff<Coo, Foo>, { age: string, gender: number }>>,
Type 'false' does not satisfy the constraint 'true'.2344Type 'false' does not satisfy the constraint 'true'.
]
 
take the challenge

解决方案

Spoiler warning // Click to reveal answer
ts
type Diff<O, O1> = {
[K in keyof O | keyof O1 as K extends keyof O & keyof O1 ? never : K]: K extends keyof O
? O[K]
: K extends keyof O1
? O1[K]
: never;
}
 
 
type Diff2<O, O1> = {
[K in keyof (O & O1) as K extends keyof (O | O1) ? never : K]: K extends keyof O
? O[K]
: K extends keyof O1
? O1[K]
: never;
}
 
ts
type Diff<O, O1> = {
[K in keyof O | keyof O1 as K extends keyof O & keyof O1 ? never : K]: K extends keyof O
? O[K]
: K extends keyof O1
? O1[K]
: never;
}
 
 
type Diff2<O, O1> = {
[K in keyof (O & O1) as K extends keyof (O | O1) ? never : K]: K extends keyof O
? O[K]
: K extends keyof O1
? O1[K]
: never;
}
 
ts
// most popular
 
type Diff<O, O1> = Omit<O & O1, keyof (O | O1)>
 
ts
// most popular
 
type Diff<O, O1> = Omit<O & O1, keyof (O | O1)>
 
view more solutions