Inference Basics 6 exercises
solution

Use Utility Types To Extract a Promise's Result

The solution here is to use the Awaited type helper to get the return value from inside the Promise:


type ReturnValue = Awaited<ReturnType<typeof getUser>>;

Another interesting thing about ReturnValue is that we are nesting calls to type helpers.

In our ReturnValue example we

Loading solution

Transcript

0:00 Here we start looking at the Awaited type helper. We're also noticing two things here. First of all, Awaited helps you unwrap a promise. It's like calling await here. Without Awaited, if I remove this, then ReturnValue is just the promise again, but if I had it back, then we get the thing that's inside the promise, very, very useful.

0:21 You'll also see that you can Nest these calls here. We end up with actually double nesting. You can do this as many times as you want. This is just calling a function, which calls another function, calls another function. Nesting these calls in here is perfectly acceptable, although, the syntax can be pretty hard to read.

0:38 If we wanted to change it, we could say, type, let's say, GetUserPromise = ReturnType this, and then do GetUserPromise inside there. Actually nesting the cause is not too bad either. This shows you the two steps you need to go through. First extract the ReturnType and then await the ReturnType.