async / await
An async function always returns a Promise. await unwraps a Promise<T> to T (and recursively unwraps nested thenables). The syntax is JavaScript; TypeScript types the unwrap.
Detailed Explanation
async and await are JavaScript. TypeScript's extra work is the return type and the type of an await expression.
An async function that returns User is typed Promise<User>. You may annotate either User (the inner type, allowed as a shorthand in some positions) or Promise<User>. The honest public annotation is Promise<User>. An async function cannot return a non-promise in its type: the async keyword wraps whatever you return in a promise.
await expr has type Awaited<typeof expr>. await on a non-promise is legal and is T. await on Promise<User> is User. await on Promise<Promise<User>> is User. That is why a missing return and a stray extra Promise.resolve show up as void or as a value you did not expect, not as a type-level crash.
Error handling: try { await p } catch (e) { } types e as unknown under useUnknownInCatchVariables (part of strict). Narrow it. There is no throws UserError syntax. If you need typed failures, return a Result or throw a class and use instanceof.
async arrow functions and methods follow the same rules. An async method on a class is Promise<T> and can override a parent method that returns Promise<T> (or, in some cases, T via thenable compatibility — do not rely on mixing sync and async overrides).
for await (const item of stream) types item as the iterator's yielded type. The async iterator interface is AsyncIterable<T> / AsyncIterator<T>. Typing a custom async iterable means implementing those interfaces, not just making the method async.
await does not make code parallel. await a(); await b(); is sequential. Promise.all is the typed way to wait together. The types of a and b stay independent inside all if you pass a tuple.
None of this inserts runtime checks on T. An async function that return res.json() as User is the same hole as any assertion.
For the microtask timing of await, see the JavaScript async/await and event-loop pages.
Code Examples
1. async wraps the return; await unwraps it.
// 1. async wraps the return; await unwraps it.
type User = { id: string };
async function loadUser(id: string): Promise<User> {
return { id };
}
async function label(id: string): Promise<string> {
const user = await loadUser(id);
return user.id;
}
console.log(await label("u1")); // u12. catch is unknown under strict.
// 2. catch is unknown under strict.
async function safeLabel(id: string): Promise<string | undefined> {
try {
return await label(id);
} catch (err) {
if (err instanceof Error) console.log(err.message);
return undefined;
}
}3. Sequential await vs Promise.all.
// 3. Sequential await vs Promise.all.
async function sequential(): Promise<[User, User]> {
const a = await loadUser("a");
const b = await loadUser("b");
return [a, b];
}
async function parallel(): Promise<[User, User]> {
return Promise.all([loadUser("a"), loadUser("b")]);
}Common Gotchas
`async` + forgotten return = `Promise<void>`
annotate the function so the mistake is local.
`await` is sequential
two independent `await`s do not overlap. Use `Promise.all` and a tuple type.
`await` on a non-promise is a no-op in types and a tiny delay in JS
do not `await` a value to "make it typed."
`as User` after `await res.json()` is not a check
validate, then return `User`.