Dev Helpers

TypeScript / Typed events

Accept a single payload field

Dev Helpers · November 18, 2025

A narrow payload type describes the exact field a function needs. Keep that contract separate from the larger database record.

Worked example

type Events = {
  start: { id: number };
  finish: { ms: number };
  failure: { reason: string };
};
console.log(((ms: Events["finish"]["ms"]) => ms / 1000)(250));

Result

0.25

Implementation notes

A narrow interface can improve reuse by avoiding dependencies on unrelated fields. Validate external payloads before passing them into the function.

Try it yourself

Pass an object literal with a misspelled key and inspect the compiler diagnostic.

Open the TypeScript example →

Editorial date November 18, 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.