Type Transformations Workshop (9 exercises)
Problem

Extract Type Arguments to Another Type Helper

In this exercise we have an interface MyComplexInterface which is acting as a type helper.

The interface takes arguments for Event, Context, Name, and Point, each being put into getEvent, getContext, getName, and getPoint, respectively.

There's also an Example that shows how MyComplexInterface is called:

Challenge

Your challenge is to create another type helper called GetPoint, where when we pass in to MyComplexInterface we return the { x: 12; y: 14} as seen in our test.

The goal is for you to focus on trying to extract the generic from the argument.

Hint: using infer will allow you to extract Point without having to change anything within MyComplexInterface.

Reference the Generics section of the TS Docs for more.

Transcript

[0:00] In this exercise, we have an interface here, which is acting as a type helper. On this interface, we have Event, Context, Name, and Point. These are all the arguments. They get put into getEvent, getContext, getName, getPoint.
[0:14] Now, we want to create another type helper called GetPoint, where when we pass in a MyComplexInterface, we return the Point that's there. We could do some interesting stuff here. Really, I want you to focus on trying to extract the generic from the generic slots in there, from the generic argument.

[0:36] This Point, what we can do is, using infer, we can actually extract that out directly without having to go into MyComplexInterface itself and grab getPoint and grab the return type or something crazy like that.

[0:49] That's your job here, is to try and find a way to use infer to extract out something from the type arguments of something that's being passed in. Good luck.