dividab / uom

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

All amounts are effectively the same type. #4

Closed geon closed 6 years ago

geon commented 6 years ago

Due to Amount being a generic, with a type parameter T extends Quantity.Quantity, all Amount s are interchangeable.

This happens since T extends Quantity.Quantity won't give you one of the types in the union Quantity.Quantity, but all types that extends any of them.

https://github.com/dividab/uom/blob/master/src/amount.ts#L11

In essence, this is totally OK:

const foo: Amount.Amount<Quantity.Temperature> = Amount.create(0, Units.Hour);

It is also very deceptive, because if you remove the explicity typing of foo, its type is correct (Amount.Amount<"Duration">), but that will still not prevent assigning it to the wrong type.

const foo = Amount.create(0, Units.Hour);
const bar: Amount.Amount<Quantity.Temperature> = foo;

References:

Jontem commented 6 years ago

I believe this is fixed now.