【发布时间】:2019-06-18 16:29:57
【问题描述】:
我有两个 HOC,想与 redux compose 一起使用,但编译器没有生成正确的类型。 Compose 声明函数来自 redux source code。如果我们将代码粘贴到playground。我们将看到不同类型的第一和第二变量。
type Func1<T1, R> = (a1: T1) => R
type Component<Props> = (props: Props) => string;
declare function compose<T1, A, R>(
f1: (b: A) => R,
f2: Func1<T1, A>
): Func1<T1, R>
declare const HOC1: <Props>(component: Component<Props>)
=> Component<Props & { prop: string }>
declare const HOC2: <Props>(component: Component<Props>)
=> Component<Props & { prop: number }>
declare const component: Component<{props: boolean}>
const first = HOC1(HOC2(component));
const second = compose(HOC1, HOC2)(component);
【问题讨论】:
-
你的游乐场链接坏了,只是一个游乐场链接,没有代码
-
@TitianCernicova-Dragomir 抱歉,我已经更新了链接。
-
我认为我们不能在当前的 typescript 类型系统中为
compose建模一个好的版本。无法将泛型类型参数捕获到 HOC。这可能适用于您非常具体的情况,但它不是一个真正的通用解决方案:declare function compose<A, R, R2>(f1: (b: A) => R,f2: (b: A) => R2,): (<P>(c: Component<P>) => Component<P & R & R2>) -
@TitianCernicova-Dragomir 很伤心。请单独发布您的答案,我会检查它是否正确。
-
添加了答案,10x
标签: reactjs typescript redux