Type Transformations Workshop (4 exercises)
solution

Use a Utility Type to Extract Function Parameters

The solution is to use the Parameters utility type, and pass it typeof makeQuery:

Note that when hovering over MakeQueryParameters, you can see that it returns a tuple with url: string and the optional opts? object just like in our tests.

Extracting Only the Second Parameter

If we wanted to extract only the second parameter, then we’d use an index type.

We can do this by creating a new type called MakeQueryParametersSecondArgument and assigning it to MakeQueryParameters[1] just like how we would access elements in an array:

In this case, passing 1 gets us the second member of the tuple, and we get everything in opts? as see when hovering over MakeQueryParametersSecondArgument.

The Parameters utility is really useful for extracting things that you don't necessarily have control of, such as code in external libraries.

Transcript

Instructor: [0:00] The solution here is to use parameters. Parameters, actually, returns a tuple here. Just like in the tests below, we end up with URL string and opts. This big, complicated piece here. If we wanted to extract only the second parameter, then we would use an index type.
[0:16] We could say, "Make query parameters second argument is parameters, or make query parameters, and then one." We're accessing the second member using the one in that tuple. We end up with method, headers, key, etc., all of this stuff.

[0:34] This is really, really useful for extracting things that you don't necessarily have control of. Maybe code in external libraries or things like this. The parameters type helper is really, really useful.