Deriving Types From Values 15 exercises
Problem

Extract the Return Type from an Async Function

We have an async fetchUser function:


const fetchUser = async (id: string) => {
return {
id,
name: "John Doe",
email: "example@email.com",
};
};

Like before, assume that this function comes from a third-party library or some other source outside our control.

We want to

Loading exercise

Transcript

00:00 Here we have a FetchUser function, and let's say we're in the same position as in our previous exercise. We want to extract out the return type from it. But FetchUser is an async function. And so instead of a user, what we're getting is a promise wrapping that user. That's pretty annoying. We're not allowed to change anything about this function.

00:18 So you can't just sort of change the async or remove it or anything like that. You have to only change the line of code with user and return type on it. So there is an awaited type helper, which is going to be of use here. And your job is to try to figure out how you can compose it with the return type type helper

00:37 in order to get this working. Because we want this user to exactly match ID, name and email down the bottom and not have this promise wrapping it.