BillTracker/client/lib/money.type-test.ts

23 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Compile-time guard for the branded money types. Not imported anywhere (so it
// never ships in a bundle); `npm run typecheck` type-checks it via the tsconfig
// include, and each `@ts-expect-error` asserts the following line IS a genuine
// type error. If the cents/dollars branding ever regresses, one of these lines
// stops erroring and typecheck fails loudly ("unused @ts-expect-error").
import { formatUSD, formatCentsUSD, asCents, asDollars, centsToDollars } from './money';
const cents = asCents(1234); // $12.34 as integer cents
const dollars = asDollars(12.34); // $12.34 as dollars
// ✅ Correct unit → compiles fine.
formatUSD(dollars);
formatCentsUSD(cents);
formatUSD(centsToDollars(cents)); // cross the boundary explicitly
// ❌ Unit mixups → compile errors (the class of bug we keep fixing).
// @ts-expect-error cents can't be formatted as dollars (would render 100× too big)
formatUSD(cents);
// @ts-expect-error dollars can't be formatted as cents
formatCentsUSD(dollars);
// @ts-expect-error a raw number must be branded (asDollars/asCents) first
formatUSD(1234);