Skip to content

All amounts are effectively the same type. #4

Closed
@geon

Description

@geon

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions