DEV Community

loading...

Discussion on: Advanced TypeScript Exercises - Question 10

Collapse
faiwer profile image
Stepan Zubashev

My solution: bit.ly/35ojBlc
code:

type Merge<A, B> = 
    & Omit<A, keyof B>
    & Omit<B, keyof A>
    & {
        [key in keyof A & keyof B]: (A[key] & B[key]) extends never
            ? A[key]
            : A[key] & B[key]
    };
Enter fullscreen mode Exit fullscreen mode

I hope I understand the task properly. Unfortunately my solution doesn't show anything except "XY" when you hover it. But it looks like it passes the test.

Collapse
faiwer profile image
Stepan Zubashev

in such a way that the latter type will overwrite types of fields of former type.

I didn't read it properly. But, BTW, my task is more interesting :)
My code really merges 2 objects. But it avoids "never" during merge. When there's never it takes the 2nd arguments value.