Inference Basics 6 exercises

Use a Utility Type to Extract Function Parameters

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

type MakeQueryParameters = Parameters<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

Loading solution


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.