I like the tricks :) – Kasia
Keep track of the currency with a generic class to avoid mixing them.
class Money<Currency extends string> {
constructor(readonly currency: Currency, readonly amount: number) {}
sum(other: Money<Currency>) {
return new Money(this.currency, this.amount + other.amount)
}
}
const dollars = new Money('USD', 2)
const euros = new Money('EUR', 3)
dollars.sum(euros) // ✅ Does not compile
Crafted with 💛 by Riccardo
Crafted with 💛 by Riccardo