gcanti / newtype-ts

Implementation of newtypes in TypeScript
https://gcanti.github.io/newtype-ts/
MIT License
582 stars 14 forks source link

Document: universal quantification #30

Open toastal opened 2 years ago

toastal commented 2 years ago

While I think the canonical example with currency is good, I think it's not quite good enough. What I want to see is how to use this with universal quantified variables. Sticking with the 'money' theme:

-- using PureScript because it expresses denser/better
newtype EUR = EUR Number
newtype USD = USD Number
newtype Withdrawal a = Withdrawal a
newtype Deposit a = Deposit a

handleUSDWithdraw ∷ Withdrawal USD → Effect Unit
handleAllDeposit ∷ ∀ a. Show a ⇒ Deposit a → Effect Unit

Lets say I have certain functions that can only handles withdrawals of USD (maybe inform the IRS), or a have a function that needs to handle all deposits (with an unwrap function give), but for all currencies (shows it in an unsafe log).

What I don't understand is how the Withdrawal a and Deposit a types are constructed with this library or how to handle them.