Beginner's TypeScript Section 18 exercises

Typing Async Functions

Let's go deeper into function types.

Here we have a createThenGetUser function:

const createThenGetUser = async (
createUser: unknown,
getUser: unknown,
): Promise<User> => {
const userId: string = await createUser();
const user = await getUser(userId);
return user;

Loading exercise


0:00 Let's go deeper into function types here. We've got a function which is createThenGetUser. It takes in a createUser function which is asynchronous. It returns a promise, and it returns a promise including a string. Then, we use that string, which is the userID we've just created from createUser, to get the user. This might be a different database call or something similar.

0:26 Both of these are currently types as unknown. Your challenge is to work out how we can type these properly, so that these errors go away, using the function syntax that we've had before and maybe using some of the promise syntax that we've had before too.