This article explains how a TypeScript branded type for integer cents eliminated an entire class of floating-point and unit-mismatch bugs across 97 files in a financial planning PWA.
•IEEE 754 floating-point cannot represent most decimal fractions exactly, so monetary values are stored as integer cents (e.g., 1999 instead of 19.99) to ensure exact arithmetic
•A branded type `Cents = number & { readonly __brand: 'Cents' }` creates a compile-time-distinct type with zero runtime overhead, enforced via a `cents()` constructor that rejects non-safe-integers
•All arithmetic operations (addMoney, subtractMoney, multiplyMoney, percentOf, sumMoney) are wrapped to preserve the Cents type and re-validate results, automatically catching overflow and division-by-zero
•`Intl.NumberFormat` instances are cached by a `locale:currency` key to avoid expensive object construction on every render
•A `ConversionResult` interface returning both `amount: Cents` and `converted: boolean` handles unavailabl
This summary was automatically generated by AI based on the original article and may not be fully accurate.