Skip to content
dreamcode
dreamcode
Map
Async types
Lesson 26 of 26
+15 XP on finish
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 every return inside the function
  • await turns Promise<Star[]> into Star[]
  • Promise.all gives 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.
index.ts
TYPESCRIPT
type-checked, then run in your browser
Console
Run your code to see its output here.
YOUR TURN

Give fetchTemp the explicit return type Promise<number>. Then write async function report() that awaits the temperatures for "Lumen" and "Nimbus" and logs their sum, 33. Call report() instead of the .then line.

Press Run to check your work.