Type Transformations Workshop (9 exercises)

Extract the Result of an Async Function

This exercise is an amalgamation of several things you've learned so far.

We have a function called getServerSideProps that doesn't have an explicit return type set:

It returns a Promise that resolves to an object containing props with the json title string.

We can see this when we hover over getServerSideProps:


Your challenge is to update InferPropsFromServerSideFunction so that it can extract whatever is put into the props object returned by getServerSideProps.

Hint: you will need to create a conditional that uses infer to check if you're extracting the right thing. Otherwise, you'll return never.


[0:00] [0: 00] We've got a bit of an interesting problem here. We have a getServerSideProps function, which we're not adding a return type to. We're just letting it infer the thing that's being passed down, so it's returning a promise with props, JSON, title.
: [0:14] [0: 14] string.

[0:14] [0: 15] We want to create a type helper here, which helps us go into typeof getServerSideProps and extract out whatever is put into props here. This means that whenever we have a getServerSideProps function, we'll be able to grab out the props from anything that we pass in.

[0:32] [0: 34] Your job here is we're going to need a conditional check. We're going to need an infer in here, and we're going to need to make sure that we grab out the right thing and return it. Otherwise, we're going to return never. There's nothing particularly new in here. This is just an amalgamation of a few things that you've learned already.