Beginner's TypeScript Tutorial (18 exercises)

Typing Promises and Async Requests

Here we have a function called fetchLukeSkywalker:

It goes to the Star Wars API at and fetches people/1, which happens to correspond to Luke Skywalker.

There's a LukeSkywalker type that includes properties based on what the API includes in the response:

However, TypeScript is showing us an error on the return type:


Your challenge is to figure out how to update the return type annotations to make TypeScript happy.


We've got a function here called fetchLukeSkywalker. What it does is it goes an API called and fetches people/1. I happen to know, because I've looked at their documentation. What I've done is I've said, "OK, that's gonna fetch LukeSkywalker." He has a name attribute, a height attribute, a mass attribute.

When I run res.json here, it's going to return me LukeSkywalker. That data attribute is going to be typed as LukeSkywalker, but it's yelling at me here because the return type of an async functional method must be called blah-blah-blah. Who knows what that means. What your challenge is is to go through and work out how I can change the type annotations here to make sure that TypeScript is happy with me.