typescript react consume context with HOC 的问题

新手上路,请多包涵

问题描述

typescript react consume context with HOC 的问题

相关代码

withNavigation = <P extends IWithNavigationProps>(WrappedComponent: React.ComponentType<P>) => {
    type IComponentWithNavigation = Omit<P, keyof IWithNavigationProps>

    const ComponentWithNavigation = (props: IComponentWithNavigation & { forwardedRef: React.Ref<{}> }) => {
      const { forwardedRef, ...restProps } = props

      return (
        <NavigationContext.Consumer>
          {
            navigationFromContext => {
              const navigation = getNavigation(navigationFromContext)

              return (
                <WrappedComponent
                  navigation={navigation}
                  {...restProps as P}
                  ref={forwardedRef}
                />
              )
            }
          }
        </NavigationContext.Consumer>
      )
    }

    ComponentWithNavigation.displayName = `withNavigation(${WrappedComponent.displayName || WrappedComponent.name})`

    return hoistNonReactStatic(forwardRef((props, ref) => (
      <ComponentWithNavigation {...props as P} forwardedRef={ref} />
    )), WrappedComponent)
  }

提示错误

Conversion of type 'Pick<Pick<P, Exclude<keyof P, "navigation">> & { forwardedRef: any; }, Exclude<Exclude<keyof P, "navigation">, "forwardedRef">>' to type 'P' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.
阅读 1.2k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题