How do you type an async function return in TypeScript?

This tests if you know async functions always return Promise<T>. A great answer: annotate Promise<User>, return User from the body, and note TypeScript implicitly wraps it. A red flag is omitting Promise and typing the return as just User.
This tests your understanding of TypeScript's implicit Promise wrapping for async functions. A strong answer covers three things in order: first, the explicit annotation must be Promise<User> because async functions always return a Promise; second, inside the function you return the plain User object and TypeScript wraps it automatically; third, if you omit Promise, the compiler will error.
Read the original → developer.mozilla.org
- #typescript
- #async-await
- #promises
- #type-system
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.