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;
Due to
Amount
being a generic, with a type parameterT extends Quantity.Quantity
, allAmount
s are interchangeable.This happens since
T extends Quantity.Quantity
won't give you one of the types in the unionQuantity.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:
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.References: