Dev Helpers

TypeScript / Branded values

Reject fractional cent amounts

Dev Helpers · December 1, 2026

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

Worked example

type Brand<T, Name extends string> = T & { readonly __brand: Name };
console.log(
  ((v: number) =>
    Number.isSafeInteger(v) ? (v as Brand<number, "Cents">) : null)(19.5),
);

Result

null

Implementation notes

A branded money type becomes useful only when construction goes through the validator. A freely scattered type assertion bypasses that boundary.

Try it yourself

Try zero, a negative whole amount, a fraction and NaN.

Open the TypeScript example →

Editorial date December 1, 2026 · Published December 6, 2026

Related articles

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.

Infer a tuple element union

Indexing a tuple's type by number yields a union of its element types. Literal inference determines how precise that union becomes.