Dev Helpers

TypeScript / Async typing

Annotate an async return value

Dev Helpers · October 21, 2025

An async function returns a promise even when its body returns an immediate value. Annotating that contract makes consumers await the correct shape.

Worked example

const load = async (id: number): Promise<{ id: number; ready: boolean }> => ({
  id,
  ready: true,
});
console.log(await load(3));

Result

{ id: 3, ready: true }

Implementation notes

A promise of an optional result differs from an optional promise. Choose the shape that makes absence easiest for the caller to handle.

Try it yourself

Change one return branch and inspect the resulting type error.

Open the TypeScript example →

Editorial date October 21, 2025 · Published December 6, 2026

Related articles

Reject fractional cent amounts

A numeric type cannot by itself guarantee whole cents. Validate integrality where money enters the typed part of the application.

Infer route names from a registry

A route registry can drive both lookup and allowed route names. Type the keys from the registry so a removed route cannot survive in a separate union.