2017-03-19 66 views
3

有打字稿錯誤如何解決打字稿Redux的連接錯誤ActionCreatorsMapObject

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已經嘗試過,但還有其他的一堆錯誤

也許你知道該

一些最佳實踐希望您的支持

+1

sessionActions在哪裏定義?你能證明這一點嗎?如果沒有看到所有的代碼,很難確定究竟有什麼問題。 – Nick

回答

6

你需要改變你的

actions: bindActionCreators(sessionActions, dispatch) 

line to

actions: bindActionCreators<any>(sessionActions, dispatch) 

或更具體的內容,因爲redux打字稿定義使用generics

See the redux typescript definitions here

<>中的泛型類型應該是相同類型的sessionActions,它也是bindActionCreators()函數調用的返回值。