-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Comments
@issuehuntfest has funded $20.00 to this issue. See it on IssueHunt |
I only started using typescript + react recently but i think you have to pass types to 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
); |
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 |
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 |
@piotrwitek has rewarded $14.00 to @piotrwitek. See it on IssueHunt
|
However, there is an error if I explicity wrap the dispatch:
How can I validate that the correct actions are being passed to
connect
?The text was updated successfully, but these errors were encountered: