Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Action creators passed to connect with bindActionCreators are not typesafe #110

Closed
C-Higgins opened this issue Nov 30, 2018 · 5 comments · Fixed by #157
Closed

Action creators passed to connect with bindActionCreators are not typesafe #110

C-Higgins opened this issue Nov 30, 2018 · 5 comments · Fixed by #157

Comments

@C-Higgins
Copy link

C-Higgins commented Nov 30, 2018

const actions = {
	test: () => 1, // Obviously not an action creator
}
const mapStateToProps = (state: RootState) => state
// No Errors
const mapDispatchToProps = (dispatch: Dispatch<RootAction>) => bindActionCreators(actions, dispatch)
const cnRoomWrapper = connect(mapStateToProps, mapDispatchToProps)(RoomWrapper)

However, there is an error if I explicity wrap the dispatch:

// Gives correct error
const mapDispatchToProps = (dispatch: Dispatch<RootAction>) => {
    test: () => dispatch(actions.test)
})

How can I validate that the correct actions are being passed to connect?

@IssueHuntBot
Copy link

@issuehuntfest has funded $20.00 to this issue. See it on IssueHunt

@WojciechMatuszewski
Copy link

WojciechMatuszewski commented Jan 22, 2019

I only started using typescript + react recently but i think you have to pass types to bindActionCreators explicitly

For example

export interface BuyAction extends Action<string> {
  type: 'BUY';
}
export interface RentAction extends Action<string> {
  type: 'RENT';
}
export const Buy: ActionCreator<BuyAction> = () => ({
  type: 'BUY'
});
export const Rent: ActionCreator<RentAction> = () => ({
  type: 'RENT'
});
type BikeActions = BuyAction | RentAction;
// --- //
const bikeActionCreators: ActionCreatorsMapObject<BikeActions> = {
  BuyAction: Buy,
  RentAction: Rent
};

const actions = {
  test: () => 1 // Obviously not an action creator
};

const mapStateToProps = (state: {}) => state;
const mapDispatchToProps = (dispatch: Dispatch<BuyAction>) =>
  bindActionCreators<BikeActions, typeof bikeActionCreators>(
    // typescript complains here, replace with bikeActionCreators to remove the error
    actions,
    dispatch
  );

@piotrwitek
Copy link
Owner

piotrwitek commented Feb 4, 2019

Hey @C-Higgins, I think you have the same issue: #6

This need to be fixed upstream, AFAICT fix is on the way.

Also you have this warning: https://github.com/piotrwitek/react-redux-typescript-guide#caveat-with-bindactioncreators

@piotrwitek piotrwitek changed the title Type of action creators passed to connect is not validated Action creators passed to connect with bindActionCreators are not typesafe Feb 4, 2019
@piotrwitek piotrwitek self-assigned this Apr 13, 2019
@piotrwitek
Copy link
Owner

Hey @C-Higgins, I solved it and added a proper solution in the guide:

// You can add extra layer of validation of your action creators
// by using bindActionCreators generic type parameter and RootAction type
const mapDispatchToProps = (dispatch: Dispatch<MyTypes.RootAction>) =>
  bindActionCreators<ActionCreatorsMapObject<Types.RootAction>>({
    invalidActionCreator: () => 1, // Error: Type 'number' is not assignable to type '{ type: "todos/ADD"; payload: Todo; } | { ... }
  }, dispatch);

You can find it in the recently added section: https://github.com/piotrwitek/react-redux-typescript-guide#connect-with-react-redux

@IssueHuntBot
Copy link

@piotrwitek has rewarded $14.00 to @piotrwitek. See it on IssueHunt

  • 💰 Total deposit: $20.00
  • 🎉 Repository reward(20%): $4.00
  • 🔧 Service fee(10%): $2.00

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants