TS EXPERTChapter 4 · TS Expert
Typing Async Code
An async function always returns a Promise, and TypeScript tracks what it resolves to: async function load(): Promise<Star[]>. await unwraps it, so the awaited value has the inner type. Promise.all keeps each position's type in a tuple, and Awaited<T> does the unwrapping at the type level. Give data-loading functions an explicit return type so every caller is checked against it.
Worked example
How it reads
Promise<Star[]>is checked against everyreturninside the functionawaitturnsPromise<Star[]>intoStar[]Promise.allgives a tuple:[Star[], number]

Cloud tip: An
async function can never return a plain value to its caller. Remember to await it, or the caller gets a Promise.

