Skip to content
loading...

Advanced TypeScript Exercises - Question 1

macsikora profile image Maciej Sikora twitter logo ・1 min read

Advanced TypeScript Exercises (20 Part Series)

1) Advanced TypeScript Exercises - Question 1 2) Advanced TypeScript Exercises - Answer 1 3 ... 18 3) Advanced TypeScript Exercises - Question 2 4) Advanced TypeScript Exercises - Answer 2 5) Advanced TypeScript Exercises - Question 3 6) Advanced TypeScript Exercises - Answer 3 7) Advanced TypeScript Exercises - Question 4 8) Advanced TypeScript Exercises - Answer 4 9) Advanced TypeScript Exercises - Question 5 10) Advanced TypeScript Exercises - Answer 5 11) Advanced TypeScript Exercises - Question 6 12) Advanced TypeScript Exercises - Answer 6 13) Advanced TypeScript Exercises - Question 7 14) Advanced TypeScript Exercises - Answer 7 15) TypeScript Exercises Bonus🦠 - Type of Pandemia 16) TypeScript Exercises Bonus🦠 - Answers Part 1 17) TypeScript Exercises Bonus🦠 - Answers Part 2 18) Advanced TypeScript Exercises - Question 8 19) Advanced TypeScript Exercises - Answer 8 20) Advanced TypeScript Exercises - Question 9

If we have a type which is wrapped type like Promise. How we can get a type which is inside the wrapped type? For example if we have Promise<ExampleType> how to get ExampleType?

Take a look at below code. Write an utility type Transform which will take a generic type argument, and if it is a Promise it will evaluate to the type inside it.

type X = Promise<string>
type Y = Promise<{ field: number }>

type ResultX = Transform<X>; // ResultX type equals string
type ResultY = Transform<Y>; // ResultY type equals { field: number }

type Transform<A> = /** here your answer **/

Post your answers in comments. Have fun! Answer will be published soon!

If you are interested in notifications about next articles please follow me on dev.to and twitter.

twitter logo DISCUSS (11)
Discussion
markdown guide
 

Nice little challenge, I wouldnt call it advanced tho, maybe more like intermediate?

Edit: heres another challenge: make a type Reverse which takes a tuple of any length and reverses its elements! (Hint: use the { 0: A; 1: B }[C extends D ? 0 : 1] hack to be able to use recursion in some cases where typescript woule've thrown an error)

 

You are really picky :). I hope in the series I will put some questions which will satisfy you. Thanks for feedback.

 

Np, check the edit to the original comment to see a challenge idea

 

Playground link. To avoid spoilers.

(On the phone so I re-wrote the example with Array)

I think the wrapper type cannot be generic. I mean the same solution wouldn't work for Promise and Array. Or at least I don't know a way.

 
 

Yep, it's sad to see typescript missing so much from not having higher kinded types!

 

Wow, I was only familiar with the approach fp-ts took, its nice to see cool stuff like this:)

 

We can do a lot already, right? But yeah, it'd be nice to have.

 

Ah, so this is where the infer keyword is useful:

type Transform<A> = A extends Promise<infer T> ? T : never; 

Basically, the infer keyword here lets us start referencing a new type, that didn't come from the original type declaration. In this case - we are returning it.

Classic DEV Post from Aug 4 '19

You're not worth hiring unless...

Maciej Sikora profile image
I am Software Developer, currently interested in static type languages (TypeScript, Elm, Reason) mostly in the frontend land