Created
August 25, 2022 12:54
-
-
Save Luisgustavom1/a66c7424a43a810a39eb7a03735a82e4 to your computer and use it in GitHub Desktop.
00645 - Medium diff (https://github.com/type-challenges/type-challenges/blob/main/questions/00645-medium-diff/README.md)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Diff< | |
Obj1, | |
Obj2, | |
SameKeys extends keyof Obj1 = keyof Obj1 & keyof Obj2 | |
> = Omit<{ | |
[K in (keyof Obj1 | keyof Obj2)]: | |
K extends keyof Obj1 | |
? Obj1[K] | |
: K extends keyof Obj2 | |
? Obj2[K] | |
: never | |
}, SameKeys> | |
type Foo = { | |
name: string | |
age: string | |
} | |
type Bar = { | |
name: string | |
age: string | |
gender: number | |
} | |
type Coo = { | |
name: string | |
gender: number | |
} | |
type Diff1 = Diff<Foo, Bar> // { gender: number } | |
type Diff2 = Diff<Bar, Foo> // { gender: number } | |
type Diff3 = Diff<Foo, Coo> // { age: string; gender: number } | |
type Diff4 = Diff<Coo, Foo> // { age: string; gender: number } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment