Beginner's TypeScript Tutorial (18 exercises)
Problem

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;
};

This function takes in an asynchronous createUser function. It returns a promise that includes a userId string.

The userId string is then used in a call to getUser which would be a database call or something similar.

Challenge

Both createUser and getUser are currently typed as unknown.

Your challenge is to type the functions so that the errors go away.

Reference the function typing and Promise syntax that we've looked at previously for help.

Transcript

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.

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.