'evolve' 函数的类型定义

dps*_*dps 9 typescript ramda.js

我正在尝试为evolveramda 中的函数编写一个简单的类型定义。(https://ramdajs.com/docs/#evolve)。官方定义无法正常工作。

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]}
Run Code Online (Sandbox Code Playgroud)

我正在尝试在通用函数中使用它:

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

但我收到一个错误:

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)
Run Code Online (Sandbox Code Playgroud)

从错误中不清楚为什么它不可分配,所以我不知道如何解决

Eri*_*ker 2

看起来发生了几件事。我能够通过提升Evolver它自己的类型别名来使其工作,这也避免了在尝试将其识别为第二个通用参数时出现索引问题。我还切换了一些返回类型,unknown让 ts 在进化器对象中推断它们。

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
  );
};
Run Code Online (Sandbox Code Playgroud)