Typing library that provides a basic type-level units arithmetics and validation.
Typing library that provides a basic type-level units arithmetics and validation.
Never [mistake metres with miles][1] and be sure of your currency conversions.
Provides bindings and helpers for usage with [BigNumber.js][2] (currently only v5).
Can be integrated with other libraries as well.
[1]: https://en.wikipedia.org/wiki/Mars_Climate_Orbiter#Cause_of_failure
[2]: https://github.com/MikeMcl/bignumber.js/
`` typescript
import { Pow, Div, Inverse, Mul, Unit } from 'ts-units';
import { ScalarBN, scalarBN } from 'ts-units/bignumber';
// Physics
type Metre = Unit<'Metre'>;
type Second = Unit<'Second'>;
type Kg = Unit<'Kg'>;
type Speed = Metre & Inverse
type Acceleration = Metre & Pow
type Force = Mul
const force = scalarBN
const gravity = scalarBN
// Oops, invalid operation. Should divide to get the mass
const mass: ScalarBN
// Type error:
// Type '["Kg", 1] | ["Metre", 2] | ["Second", -4]' is not assignable to type '["Kg", 1]'.
// Ok
const mass: ScalarBN
// To avoid errors values with different units cannot be compared or added:
force.add(gravity) // ???
force.gt(gravity)
// Type error:
// Type 'Acceleration' is not assignable to type 'Mul
// Property 'Kg' is missing in type 'Acceleration'. [2345]
// Finance
type USD = Unit<'USD'>;
type ETH = Unit<'ETH'>;
type BTC = Unit<'BTC'>;
function getETHpriceInUSD(): ScalarBN
function getBTCpriceInUSD(): ScalarBN
function logPriceBTCinETH(price: ScalarBN
// Ooops, what actually should be divided by what?
logPriceBTCinETH(getETHpriceInUSD().div(getBTCpriceInUSD()));
// Type error:
// Types of property 'ETH' are incompatible.
// Type '-1' is not assignable to type '1'. [2345]
// Ok!
logPriceBTCinETH(getBTCpriceInUSD().div(getETHpriceInUSD()));
`
& can combine only types with no overlapping units. Use Mul` to perform generic unit multiplication.