8000 Needs to specify types for configureMeasurements if configure more than 2 measures · Issue #221 · convert-units/convert-units · GitHub
[go: up one dir, main page]

Skip to content

Needs to specify types for configureMeasurements if configure more than 2 measures #221

@ZengLawrence

Description

@ZengLawrence

I got a Typescript syntax error for following code similar to what is in the readme page

const convert = configureMeasurements({
    volume,
    mass,
});

Syntax error:

Type 'Measure<MassSystems, MassUnits>' is not assignable to type 'Measure<MassSystems, VolumeUnits>'.
  The types of 'systems.metric' are incompatible between these types.
    Type 'Partial<Record<MassUnits, Unit>> | undefined' is not assignable to type 'Partial<Record<VolumeUnits, Unit>> | undefined'.
      Type 'Partial<Record<MassUnits, Unit>>' has no properties in common with type 'Partial<Record<VolumeUnits, Unit>>'.ts(2322)

Typescript compiler seemed to be confused with generic types in this closure and could not figure out what went with what. All three types TMeasures, TSystems and TUnits were linked. It would not make sense to mix and match them.

Here is the work-around that got rid of the syntax error:

const convert = configureMeasurements<"volume" | "mass", VolumeSystems | MassSystems, VolumeUnits | MassUnits>({
  volume,
  mass,
});

As you could see, it was a bit awkward to use. Could it be possible to redefine all of them as a single type and make it easier for Typescript to figure out?

Typescript version: 4.2.4
convert-units version: 3.0.0-beta.2

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

      0