typed-typings / npm-ramda

TypeScript's type definitions for Ramda
MIT License
384 stars 64 forks source link

Evolve typings #447

Open icaroponce opened 4 years ago

icaroponce commented 4 years ago

Hi there,

I'm having issues trying to type the evolve method when the properties' type changes. E.g. string changes to number as seen below:

const t  = {
  price: v => (v ? parseFloat(v) : null),
  track: v => (v ? 'ON' : 'OFF'),
};

type T = {
  price: string | null;
  track: boolean;
};

const i: T = {
  price: '111',
  track: true,
};

const e = evolve(t, i);

Trying to type the transformations object with:

const t: {
  price: Morphism<string | null, number | null>;
  track: Morphism<boolean, 'ON' | 'OFF'>;
} = {...}

gives this error back

[tsserver 2769] [E] No overload matches this call.
  Overload 1 of 6, '(_transformations: Placeholder, object: T): evolve_01<unknown, T>', gave the following error.
    Argument of type '{ price: Morphism<string | null, number | null>; track: Morphism<boolean, "ON" | "OFF">; }' is not assignable to parameter of type 'Placeholder'.
      Property '"@@functional/placeholder"' is missing in type '{ price: Morphism<string | null, number | null>; track: Morphism<boolean, "ON" | "OFF">; }' but required in type 'Placeholder'.
  Overload 2 of 6, '(transformations: Evolver<{ price: number | null; track: "ON" | "OFF"; }>, object: { price: number | null; track: "ON" | "OFF"; }): { price: number | null; track: "ON" | "OFF"; }', gave the following error.
    Argument of type '{ price: Morphism<string | null, number | null>; track: Morphism<boolean, "ON" | "OFF">; }' is not assignable to parameter of type 'Evolver<{ price: number | null; track: "ON" | "OFF"; }>'.
      Type '{ price: Morphism<string | null, number | null>; track: Morphism<boolean, "ON" | "OFF">; }' is not assignable to type '{ price?: number | Morphism<number | null, number | null> | null | undefined; track?: "ON" | "OFF" | Morphism<"ON" | "OFF", "ON" | "OFF"> | undefined; }'.
        Types of property 'price' are incompatible.
          Type 'Morphism<string | null, number | null>' is not assignable to type 'number | Morphism<number | null, number | null> | null | undefined'.
            Type 'Morphism<string | null, number | null>' is not assignable to type 'Morphism<number | null, number | null>'.
              Type 'number | null' is not assignable to type 'string | null'.
                Type 'number' is not assignable to type 'string | null'.

Any ideas about what is going wrong here?

Thanks.