Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type definition for 'evolve' function

I'm trying to write a simple type definition for evolve function from ramda. (https://ramdajs.com/docs/#evolve). Official definition does not work correctly.

type Transformation<State> = {
  [Key in keyof State]: (x: State[Key]) => any
}

declare function evolve
  <State extends {}, Evolver extends Partial<Transformation<State>>>(evolver: Evolver, state: State):
  {[Key in keyof State]: Evolver[Key] extends (...args: any[]) => {} ? ReturnType<Evolver[Key]> : State[Key]}

I'm trying to use that in a generic function:

const foo = <State extends {a: string, b: string}>(state: State) => {
  const test = evolve({
    a: x => x,
    b: x => x
  }, state)
}

but I'm getting an error:

Argument of type '{ a: (x: State["a"]) => State["a"]; b: (x: State["b"]) => State["b"]; }' is not assignable to parameter of type 'Partial<Transformation<State>>'.(2345)

It's not clear from the error why it's not assignable, so I don't know how to fix that

like image 650
dps Avatar asked Oct 15 '20 09:10

dps


People also ask

What is evolution in biology?

Evolution, theory in biology postulating that the various types of living things on Earth have their origin in other preexisting types and that the distinguishable differences are due to modifications in successive generations.

What is the type of a function?

A function type depends on the type of the parameters and the result type of the function (it, or more accurately the unapplied type constructor · → ·, is a higher-kinded type ).

What is the evolution trend in the species?

The evolution trend in the species was first explained by Charles Darwin to be based on the ‘theory of natural selection.’ This theory forms a solid base for evolution seen in the various life forms on earth. Organisms evolve from inheriting some of their physical or behavioral traits.

What is the central idea of the theory of evolution?

…centre of the theory of evolution as proposed by Charles Darwin and Alfred Russell Wallace were the concepts of variation and natural selection. Hereditary variants were thought to arise naturally in populations, and then these were either selected for or against by the contemporary environmental conditions.


1 Answers

It looks like there was a couple things going on. I was able to get this to work by lifting Evolver in it's own type alias which also avoids an index issue when trying to identify it as the 2nd generic param. I also switched some return types to unknown to have ts infer them in the evolver object.

type Transformation<T> = {
  [K in keyof T]: (x: T[K]) => unknown;
};

type Evolver<State> = Partial<Transformation<State>>;

declare function evolve<State extends { [key: string]: unknown }>(
  evolver: Evolver<State>,
  state: State
): {
  [Key in keyof State]: Evolver<State>[Key] extends (...args: any[]) => infer R
    ? R
    : State[Key];
};

const foo = (state: { a: string; b: string }) => {
  const test = evolve(
    {
      a: (x) => x,
      b: (x) => x,
    },
    state
  );
};
like image 107
Eric Tucker Avatar answered Sep 28 '22 03:09

Eric Tucker