Reputation: 81
I am looking to hook up React Redux to my component in typescript. I am using these docs as a template.
I have the following type defining the state I have in the Redux store (there is only one):
export interface rootState {
favourites: ImageThumbnail[]
}
My component:
...
interface StateProps {
favourites: NasaImageThumbnail[]
}
interface DispatchProps {
addFavourite: () => void
removeFavourite: () => void
}
interface OwnProps {
...
}
const mapState = (state: rootState) => ({
favourites: state.favourites
})
const mapDispatch = {
addFavourite: () => ({ type: 'ADD_FAVOURITE' }),
removeFavourite: () => ({ type: 'REMOVE_FAVOURITE' })
}
type combinedProps = StateProps & DispatchProps & OwnProps
//Component body
export default connect<StateProps, DispatchProps, OwnProps>(
mapState,
mapDispatch
)(Component)
mapState within the connect function is producing the following error:
No overload matches this call. The last overload gave the following error. Argument of type '(state: rootState) => { favourites: ImageThumbnail[]; }' is not assignable to parameter of type 'MapStateToPropsParam<StateProps, OwnProps, DefaultRootState>'. Type '(state: rootState) => { favourites: ImageThumbnail[]; }' is not assignable to type 'MapStateToPropsFactory<StateProps, OwnProps, DefaultRootState>'.
Upvotes: 0
Views: 254
Reputation: 67469
Don't pass all those generic args to connect
. They should generally be inferred.
In fact, per that page, we specifically recommend using the ConnectedProps<T>
technique to infer the type of props passed from connect
to your component.
You should also note that:
RootState
type from your root reducer or storeconnect
, and one of the reasons is that it's easier to use with TypeScriptUpvotes: 2