dividab / uom

Extensible unit of measure conversion with type safety for typescript
MIT License
18 stars 1 forks source link

Conditional types for UnitTimes and UnitDivide #22

Open jonaskello opened 5 years ago

jonaskello commented 5 years ago

While it is possible to generically divide any unit by any other unit and get a new unit that works for conversion, it is not possible to get the type of the new unit because we don't know which quantity it should get. To solve that UnitTimes and UnitDivide exists and they support dividing certain units by other units, and returning a correctly typed unit in the resulting quantity.

However it is still not possible to have a generic Amount.divide or Amount.times.

This might be solvable by using conditional types in typescript. Something like:


type Divide<Q1, Q2> = Q1 extends Length ? 
  Q2 extends Duration ? Velocity : 
  Q2 extends Length ? Dimensionless : never : never.