Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 23, 2020 12:13 pm GMT

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 nevertype 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.


Original Link: https://dev.to/macsikora/advanced-typescript-exercises-question-10-2n93

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To