Last active
May 7, 2022 12:18
-
-
Save jfet97/45840ab2dcf7012911688fd31d1f9861 to your computer and use it in GitHub Desktop.
Merge an union of objects with different types for the same properties
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 AllKeys<T> = T extends unknown ? keyof T : never | |
// type Idx<T, K extends PropertyKey> = T extends any ? T[K & keyof T] : never | |
type Idx<T, K extends PropertyKey> = T extends unknown ? K extends keyof T ? T[K] : never : never; | |
type MyMerge<T> = { [K in AllKeys<T>]: Idx<T, K> } | |
// TODO: result of test case must be `{foo: 1 | 2; bar: 3}` | |
type TestCase1 = MyMerge<{ foo: 1 } | { foo: 2; bar: 3 }>; | |
// TODO: result of test case must be `{foo: 1 | 2 | 7; bar: 3 | 8}` | |
type TestCase2 = MyMerge< | |
{ foo: 1 } | { foo: 2; bar: 3 } | { foo: 7; bar: 8 } | |
>; | |
// TODO: result of test case must be `{foo: 1 | 2; bar: 3 | 8}` | |
type TestCase3 = MyMerge< | |
{ foo: 1 } | { foo: 2; bar: 3 } | { bar: 8 } | |
>; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment