DEV Community

Notes on TypeScript: Conditional Types

A. Sharif on February 17, 2019

Introduction These notes should help in better understanding TypeScript and might be helpful when needing to lookup up how leverage Type...
Collapse
 
grossbart profile image
Peter Gassner

Thanks for the writeup! There are two spots where I got stuck and I think they are related.

1.

type RemoveUndefinable<Type> = {
  [Key in keyof Type]: undefined extends Type[Key] ? never : Key
}[keyof Type];

2.

type GetPropertyTypes<Type> = Type extends {a: infer U, b: infer U} ? U : never;

In 1. I got confused by the [keyof Type] at the end of the expression; how does it work?

In 2. I got confused by having the same type variable U used twice in different spots returning different values.

There is some form of relationship-forming happening where I can‘t follow because I don‘t know how the compiler works out a solution. Could you expand a bit on this and maybe show step by step how the compiler arrives at a solution? Now that I think of this I believe that understanding this is fundamental to getting how the whole type system works 🤔

Collapse
 
busypeoples profile image
A. Sharif

Thanks for the feedback!
Will try to update the post with further explanations.

Collapse
 
neewbee profile image
stephen

This is so helpful, thank you!

Collapse
 
perjerz profile image
JaMe Siwat Kaolueng

Nice article. Thank you very much. Really want to know about pre-defined type more.