taro icon indicating copy to clipboard operation
taro copied to clipboard

@tarojs/redux 的 types 文件需要修改

Open PoiScript opened this issue 7 years ago • 6 comments

问题描述

@tarojs/redux 的 types 文件在 connect 函数的相关定义中有问题:

export type InferableComponentEnhancerWithProps<IInjectedProps, INeedsProps> =
    <IComponent extends ComponentClass<IInjectedProps & INeedsProps>>(component: IComponent) => IComponent

export interface IConnect {
  <IStateProps = {}, IDispatchProps = {}, IOwnProps = {}, IStore = any>(
      mapStateToProps?: MapStateToPropsParam<IStateProps, IOwnProps, IStore>,
      mapDispatchToProps?: MapDispatchToPropsParam<IDispatchProps, IOwnProps>,
  ): InferableComponentEnhancerWithProps<IStateProps & IDispatchProps, IOwnProps>
}

其中 InferableComponentEnhancerWithProps 接受的参数和返回值都是 IComponent ,而实际上应该是不同的,因为 IInjectedProps 是经过函数之后注入的 props 而 INeedsProps 是本来就存在的 props。

应该改成类似以下的定义:

export type InferableComponentEnhancerWithProps<IInjectedProps, INeedsProps> =
    <IComponent extends ComponentClass<IInjectedProps & INeedsProps>>(component: IComponent)
        => ComponentClass<INeedsProps>

可以参考 @types/react-redux 的定义:

// Injects props and removes them from the prop requirements.
// Will not pass through the injected props if they are passed in during
// render. Also adds new prop requirements from TNeedsProps.
export interface InferableComponentEnhancerWithProps<TInjectedProps, TNeedsProps> {
    <P extends TInjectedProps>(
        component: Component<P>
    ): ComponentClass<Omit<P, keyof TInjectedProps> & TNeedsProps> & {WrappedComponent: Component<P>}
}

PoiScript avatar Jun 16 '19 07:06 PoiScript

欢迎提交 Issue~

如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏

如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。

Good luck and happy coding~

taro-bot avatar Jun 16 '19 07:06 taro-bot

欢迎 PR 咯~

Pines-Cheng avatar Jun 17 '19 01:06 Pines-Cheng

返回值应该是ComponentType类型的 type ComponentType<P = {}> = ComponentClass<P> | FunctionComponent<P>

vonweb avatar Jun 17 '19 07:06 vonweb

WechatIMG35 同样有这个需求,希望官方支持下

l1Dan avatar Sep 09 '19 19:09 l1Dan

从@tarojs/redux引入的connect,在连接函数式组件时,InferableComponentEnhancerWithProps函数返回值应该是ComponentType类型,而不是只有ComponentClass类型,不然会一直提示类型错误

engineerzy avatar Nov 21 '19 06:11 engineerzy

Taro 3 可以直接使用 redux,不需要引入 @tarojs/redux 了

Chen-jj avatar Jul 10 '20 11:07 Chen-jj