TypeScript Patterns That Prevent Financial Bugs
TypeScript Patterns That Prevent Financial Bugs
Financial software has a unique property: small type errors become large monetary errors. Swapping a debit for a credit, confusing cents for dollars, passing the wrong currency code — these aren't cosmetic issues. TypeScript can catch many of these at compile time if you use the right patterns.
Branded Types for Domain Primitives
A number representing cents and a number representing dollars are the same type to TypeScript but wildly different to your accounting department. Branded types create compile-time distinctions:
// typescripttype Cents = number & { readonly __brand: 'Cents' }; type Dollars = number & { readonly __brand: 'Dollars' };
Now you can't accidentally pass cents to a function expecting dollars. The compiler catches it. No runtime overhead, no additional memory — just type safety.
Exhaustive Matching for Transaction States
Every transaction goes through a state machine: pending → authorized → captured → settled. Each state has different available operations. Exhaustive switch statements ensure you handle every state and that adding a new state breaks the build until every handler is updated.
Opaque Types for External IDs
Payment IDs, user IDs, and transaction IDs should never be interchangeable, even if they're all strings. Opaque types prevent passing a user ID where a payment ID is expected. Combine this with branded types and you've eliminated an entire category of "wrong ID" bugs that are notoriously hard to debug in production.
$ /related
Financial Audit Logs: What to Record and What Regulators Want
A practical guide to building audit logging systems that satisfy PCI-DSS, SOC 2, and regional banking regulations without over-engineering.
Rate Limiting a Payment Gateway in Production
Lessons learned implementing rate limiting on a high-throughput payment gateway — the algorithms, the edge cases, and the production incidents.
Docker Multi-Stage Builds for Fintech Microservices
How multi-stage Docker builds reduce image sizes by 80% for fintech microservices while maintaining security compliance requirements.