【问题标题】:Remove prop injected by HOC from the Typescript interface of the component returned by HOC?从 HOC 返回的组件的 Typescript 接口中移除 HOC 注入的 prop?
【发布时间】:2021-02-05 08:46:40
【问题描述】:

我正在尝试制作一个高阶组件,它从当前上下文中获取一个函数并将其注入到包装组件中的一个道具中,并且仍然保持Props 接口。

我是这样包装的:

interface Props extends AsyncRequestHandlerProps {
  bla: string;
}

class MyComponent extends React.Component<Props> {
 // ....
}

export default withAsyncRequestHandler(MyComponent)

我已经像这样定义了withAsyncRequestHandler

export interface AsyncRequestHandlerProps {
  asyncRequestHandler: <T>(promise: Promise<T>) => Promise<T | null>;
}

type PropsWithoutInjectedHandler<P> = Omit<P, keyof AsyncRequestHandlerProps>;


export function withAsyncRequestHandler<P>(Component: React.ComponentType<P>) {
  return class ComponentWithAsyncRequestHandler extends React.Component<
    PropsWithoutInjectedHandler<P>
  > {
    static contextType = AsyncHandlerContext;
    context!: AsyncHandlerContext | null;
    render = () => {
      const asyncRequestHandler: <T>(
        promise: Promise<T>
      ) => Promise<T | null> = (promise) => {
        if (this.context === null) {
          throw new Error(
            "withAsyncRequestHandler should only wrap components that are mounted inside <AsyncHandler />."
          );
        }
        return AsyncRequest(promise, this.context);
      };
      const { ...props } = this.props;
      return (
        <Component
          {...props}
          asyncRequestHandler={asyncRequestHandler}
        ></Component>
      );
    };
  };
}

MyComponent 的直接签名与 bla 属性和 asyncRequestHandler 属性有关。我想要的是包装器 HOC 将返回一个只有 bla 属性的组件签名,因为 asyncRequestHandler 已被注入。

这个 HOC 的外部接口似乎可以工作,我仍然可以在安装包装组件时从 typescript 中获取剩余的 props。

但是在 HOC 内部我得到一个错误:

我当前的代码在我将&lt;Component&gt; 挂载到render() 的那一行出现了这个错误。

Type 'Readonly<Pick<P, Exclude<keyof P, "asyncRequestHandler">>> & { asyncRequestHandler: <T>(promise: Promise<T>) => Promise<T | null>; children?: ReactNode; }' is not assignable to type 'IntrinsicAttributes & P & { children?: ReactNode; }'.
  Type 'Readonly<Pick<P, Exclude<keyof P, "asyncRequestHandler">>> & { asyncRequestHandler: <T>(promise: Promise<T>) => Promise<T | null>; children?: ReactNode; }' is not assignable to type 'P'.
    'P' could be instantiated with an arbitrary type which could be unrelated to 'Readonly<Pick<P, Exclude<keyof P, "asyncRequestHandler">>> & { asyncRequestHandler: <T>(promise: Promise<T>) => Promise<T | null>; children?: ReactNode; }'.ts(2322)

我猜问题出在Omit&lt;P, keyof AsyncRequestHandlerProps&gt; 构造,以及它的用法上?

【问题讨论】:

    标签: reactjs typescript react-props higher-order-components


    【解决方案1】:

    根据https://github.com/Microsoft/TypeScript/issues/28938#issuecomment-450636046,这是 TS 中的一个错误。

    从 3.2 开始,泛型扩展运算符的行为发生了变化。显然,props 的类型会作为负面影响被删除,但您可以通过在传播回包装组件时使用 {...props as P} 将其转换回 P 来解决此问题。

    按照建议,试试这个:

    <Component
          {...props as P}
          asyncRequestHandler={asyncRequestHandler}
    />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-02
      • 2021-03-10
      • 2017-11-04
      • 1970-01-01
      • 2019-10-12
      • 2020-10-09
      • 2020-01-16
      • 1970-01-01
      相关资源
      最近更新 更多