如何简化此撰写类型?

时间:2019-05-07 17:49:48

标签: typescript

我有一个compose函数,它构成React HOC并通过fns向下复制静态信息。

我可以使用它,但是类型绝对是一团糟,我想知道是否有人对TS的了解比我好?

// from a utility lib
type SetDifference<A, B> = A extends B ? never : A;
type Diff<T extends object, U extends object> = Pick<
  T,
  SetDifference<keyof T, keyof U>
>;

// the fn signature
export function compose<A, B extends object, C extends object, D extends object, E extends object>(
  f1: (_: A) => B,
  f2: (_: B) => C,
  f3: (_: C) => D,
  f4: (_: D) => E,
): <T extends object>(
  _: T,
) => T &
  Diff<E, T> & Diff<Diff<D, E>, T> & Diff<Diff<Diff<C, D>, E>, T> & Diff<Diff<Diff<Diff<B, C>, D>, E>, T>

该类型基本上就是您会从中得到的:{...B, ...C, ...D, ...E, ...T}

有什么想法吗?

ts playground

0 个答案:

没有答案