gcanti / money-ts

TypeScript library for type-safe and lossless encoding and manipulation of world currencies and precious metals
https://gcanti.github.io/money-ts/
MIT License
90 stars 8 forks source link

ExchangeRate types don't match implementation #10

Open felixSchl opened 3 years ago

felixSchl commented 3 years ago

The type of exchange is currently

export const exchange = <S extends string, D extends string>(er: ExchangeRate<S, D>) => (d: Dense<S>): Dense<D> => {
  return unsafeCoerce(d.mul(unwrap(er)))
}

The problem is that Dense<D> also carries runtime information for D. In the implementation above this information is not adapter, so you end up with a value thinking it's Dense<D> when at runtime it's still Dense<S>. You can see this in effect when trying to round the resulting Dense<D> - it will try to look up the target unit under source format's scale table.