DEV Community

Cover image for Advanced TypeScript Exercises - Question 10

Advanced TypeScript Exercises - Question 10

Pragmatic Maciej on October 23, 2020

Intersection type level operator & has changed in the last versions of TypeScript. The current behavior escalates 'never' type, so if any of fi...
Collapse
 
hnicolas profile image
Nicolas Hervé •

I curious to know if there is a simpler solution.
typescriptlang.org/play?#code/PTAE...

Collapse
 
sirmoustache profile image
SirMoustache •

Here is mine: Playground

Collapse
 
macsikora profile image
Pragmatic Maciej •

Really neat one 👍

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.

Collapse
 
nmonastyrskyi profile image
Nikita Monastyrskiy •

It was a great series of exercises! Thank you, Maciej!

Here is my last solution