【问题标题】:How to resolve typescript Redux connect error ActionCreatorsMapObject如何解决 typescript Redux 连接错误 ActionCreatorsMapObject
【发布时间】:2017-03-19 15:08:52
【问题描述】:

打字稿出错

ERROR in [at-loader] ./src/app/components/profile/userProfile/_userMain.tsx:130:37 
    TS2345: Argument of type 'typeof "/usr/src/app/src/js/src/app/redux/actions/sessionActions"' is not assignable to parameter of type 'ActionCreatorsMapObject'.

ERROR in [at-loader] ./src/app/components/profile/userProfile/_userMain.tsx:133:124 
    TS2345: Argument of type 'typeof UserProfileMain' is not assignable to parameter of type 'ComponentClass<(state: any) => any> | StatelessComponent<(state: any) => any>'.
  Type 'typeof UserProfileMain' is not assignable to type 'StatelessComponent<(state: any) => any>'.
    Type 'typeof UserProfileMain' provides no match for the signature '(props: ((state: any) => any) & { children?: ReactNode; }, context?: any): ReactElement<any>'

没有装饰器可以解决这个问题吗? (@connect)

interface IActions {
    avatarUpload: (file:any) => void;
}

export interface IMainProps {
    auth: number,
    actions: IActions
}

class UserProfileMain extends React.Component<IMainProps, any> {
    constructor(props:any){
        super(props);
        this.state ={
            files: [],
        }
    }

    render(){
        return(<div />)
    }
}

const mapStateToProps = (state:any):any => {
    return {
        auth: state.authStatus.data.user.id,
    };
};

const mapDispatchToProps = (dispatch:any):any => {
    return {
        actions: bindActionCreators(sessionActions, dispatch) //First error (130:37)
    };
}
export default connect<typeof mapStateToProps, typeof mapDispatchToProps, IMainProps>(mapStateToProps, mapDispatchToProps)(UserProfileMain); //second error (133:124)

已经尝试使用UserProfileMain as any,但遇到了其他一堆错误

也许你知道一些最佳实践

希望大家多多支持

【问题讨论】:

  • sessionActions 是在哪里定义的?你能展示一下吗?如果不查看所有代码,很难确定到底是哪里出了问题。

标签: reactjs typescript compiler-errors redux


【解决方案1】:

你需要改变你的

actions: bindActionCreators(sessionActions, dispatch)

行到

actions: bindActionCreators<any>(sessionActions, dispatch)

或更具体的东西,因为 redux 打字稿定义使用generics

See the redux typescript definitions here.

中的泛型类型应该和sessionActions的类型相同,也是bindActionCreators()函数调用的返回值。

【讨论】:

    【解决方案2】:

    你可能需要

    bindActionCreators<any, any>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-19
      • 2019-01-09
      • 2017-06-08
      • 1970-01-01
      • 2012-05-20
      • 1970-01-01
      • 2023-01-05
      • 2013-03-28
      相关资源
      最近更新 更多