Advanced TypeScript Exercises - Question 10

macsikora

Pragmatic Maciej

Posted on October 23, 2020

Advanced TypeScript Exercises - Question 10

Intersection type level operator & has changed in the last versions of TypeScript. The current behavior escalates 'never' type, so if any of fields will produce empty/never type, the whole composite will end as 'never'. Let's see some examples:

type X  = {
    a: 1
    b: number
}
type Y = {
    a: 2
    b: string
    c: boolean
}
// XY is never, as field 'a' evaluates as 1 & 2 which is never
type XY = X & Y 
Enter fullscreen mode Exit fullscreen mode

More about this TS behavior you can find here:

Exercise will be about having different intersection behavior. Our task is to write Merge type level function which will merge two product/object types. Our final Merge should be able to create a type from above X and Y in such a way that the latter type will overwrite types of fields of former type.

type XY = Merge<X,Y>
// XY should be {a: 2, b: string, c: boolean}
Enter fullscreen mode Exit fullscreen mode

Link to the playground with the task.

Good luck! If you have a solution then don't hesitate to link it in the comment. Answer will be published soon!

This series will continue. If you want to know about new exciting questions from advanced TypeScript please follow me on dev.to and twitter.

💖 💪 🙅 🚩
macsikora
Pragmatic Maciej

Posted on October 23, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related