我有一个Redux商店,想要连接它。这里是我的容器组件的摘录:

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'.

我正在使用@ types/react-redux @ 4.4.44,它公开了MapStateToProps接口(interface)。我会认为我的mapStateToProps遇到了此接口(interface)...但是出了点问题。有什么想法吗?

最佳答案

ownProps没有任何类型。您必须给它一个类型。

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

关于reactjs - 正确的Redux connect的mapStateToProps类型声明(TypeScript 2),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44720293/

10-12 01:27