Typed Promises and Async Code
- Promise<T>. A Promise<T> names what eventually arrives: await unwraps it back to T with the checker watching.
- Async return types. An async function always returns a promise: annotate the payload: async load(): Promise<User[]>.
- Typed errors. A caught error is unknown under strict options: prove what it is before reading its message.
API Response Types
- Modelling responses. Write a type for each API response shape: the compiler then tracks that shape through your whole app.
- Nullable fields. Model what the server truly sends: fields that can be null must say so, or the crash arrives in production.
- Versioned shapes. When the API changes, change the type first: every place that must adapt lights up as an error.
Runtime Validation vs Compile-Time Types
- The trust boundary. A cast on a fetch result is a promise you did not verify: external data must be checked at runtime.
- Schema validation. A schema validator checks the real payload and hands back a typed value: parse, don’t assert.
- Single source of truth. Derive the static type FROM the schema, so runtime check and compile-time type can never drift apart.
Before and after this level
- Before: Level 9: Compiler & Configuration: tsconfig, Strictness and Compiler Options, Build Targets and Module Formats.
- Next: Level 11: Libraries & Declarations: Declaration Files, Third-Party Type Definitions, Publishing Typed Libraries.
In the app each of these ideas is taught and then asked back, and the level ends with a checkpoint and flashcards. See Checkpoints and flashcards.
Frequently asked questions
What does TypeScript Level 10: Async & External Data cover?
Typed Promises and Async Code, API Response Types, Runtime Validation vs Compile-Time Types: 3 lessons and 9 ideas.
What should I know before this level?
The level before it, Level 9: Compiler & Configuration, which covers tsconfig, Strictness and Compiler Options, Build Targets and Module Formats.