2017-06-23 105 views
3

我有一個Redux存儲並希望將其連接。這裏從我的容器成分的提取物:Redux connect(TypeScript 2)的mapStateToProps的正確類型聲明

interface IProps { 
    states: IAppState; 
    actions: IAppProps; 
} 
// mapping state to the props 
const mapStateToProps = (state: IAppState, ownProps = {}) => ({ 
    states: state 
}); 
// mapping actions to the props 
const mapDispatchToProps = (dispatch: Redux.Dispatch<IAppState>) => ({ 
    actions: Redux.bindActionCreators(actions, dispatch) 
}); 

// connect store to App 
@connect<IAppState, IAppProps, any>(
    mapStateToProps, 
    mapDispatchToProps 
) 
export default class App extends React.Component<IProps, {}> { 
//... 
} 

編譯時我收到了以下問題:我使用的是

error TS2345: Argument of type '(state: IAppState, ownProps?: {}) => { states: IAppState; }' is not assignable to parameter of type 'MapStateToPropsParam<IAppState, any>'. 
    Type '(state: IAppState, ownProps?: {}) => { states: IAppState; }' is not assignable to type 'MapStateToPropsFactory<IAppState, any>'. 
    Type '{ states: IAppState; }' is not assignable to type 'MapStateToProps<IAppState, any>'. 
     Type '{ states: IAppState; }' provides no match for the signature '(state: any, ownProps?: any): IAppState'. 

@類型/反應,終極版@ 44年4月4日暴露MapStateToProps接口。我會認爲我的mapStateToProps面對這個接口...然而有些事情是錯誤的。任何想法是什麼?

回答

1

嗯,好像@types/react-redux接受mapStateToProps作爲參加了,我想它像{ states: AppStateTree }映射過程中修改它道具確切返還相同種類。相反,我修改了狀態樹combineReducers({ states: myReducer })。所以這個代碼工作正常。

interface IRootState { 
    state: IAppState; 
} 

const mapStateToProps = (state: IRootState) => state; 

const mapDispatchToProps = { 
    toggleOpenAddFeed 
}; 

type IProps = IRootState & typeof mapDispatchToProps; 

class App extends React.Component<IProps, {}> { 
render() { 
    return (
     <div className="main-wrapper"> 
     <Mycomponent store={this.props} /> 
     </div> 
    ); 
    } 
} 

export default connect(mapStateToProps, mapDispatchToProps)(App); 
0

ownProps沒有任何類型。你必須給它一個類型。

const mapStateToProps = (state: IAppState, ownProps: any = {}) 
+0

謝謝你的概念,但它並沒有解決我的問題:( –

+0

嗯這是奇怪的例外看,它看起來像打字稿不喜歡你如何定義'狀態'。試着看看那個...... – Ematipico

相關問題