Grokking TypeScript
0% completed
Working with Async/Await
Leaving out one word in the code below produces a compiler error. The fetchUser function here is the one from the previous lesson, and it returns a Promise<User>.
async function showUserName() { const user = fetchUser(1); console.log(user.name); }
error TS2339: Property 'name' does not exist on type 'Promise<User>'.
The variable user holds a promise and not a user, and a promise has no name property. The missing word is await, and adding it changes the type of the expression to User:
async function showUserName() {
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%