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

Add Dispatch overload to Redux module. #247

Closed
wants to merge 1 commit into from
Closed

Add Dispatch overload to Redux module. #247

wants to merge 1 commit into from

Conversation

threehams
Copy link

Similarly to the bindActionCreators overload, this extends Redux to dispatch thunks and provide the correct return values. This prevents us from needing to build thunk support into react-redux.

I could use some advice on testing this one. The following is an error, as it appears that this Dispatch import receives the interface before the redux-thunk declarations are merged:

import { Dispatch } from "redux";
type ExtendedDispatch = Dispatch<ThunkResult<boolean>>;

Understanding what's happening here might help me remove inlined thunk logic from connect() typings in react-redux.

I've tested this in a real-world application with react-redux hooks typings (https://github.com/MrWolfZ/DefinitelyTyped/pull/3) in two forms:

const thunkAction = () => {
  return (dispatch: Dispatch, getState: () => State) => {
    return dispatch({ type: "SET_STATE" });
  };
};
const dispatch = useDispatch();
// typeof result === { type: "SET_STATE" }
const result = dispatch(thunkAction()) 

// typeof result === { type: "SET_STATE" }
const result = dispatch((thunkDispatch, getState) => { // correctly infers dispatch + getState
  return thunkDispatch();
});

@demian85
Copy link

demian85 commented Jul 2, 2019

What is the status of this?

@threehams
Copy link
Author

The status is that it doesn't work, I don't know why it doesn't work, and I'm trying to set up a minimal repo to make it easier to understand the interaction between redux + libraries that extend redux.

@ValeryVS
Copy link

@threehams
I think, I solved this

declare module "redux" {
  import {ThunkAction} from "redux-thunk";

  /*
   * Overload to add thunk support to Redux's dispatch() function.
   * Useful for react-redux or any other library which could use this type.
   */
  export interface Dispatch<
    TBasicAction extends Action,
  > {
    <TReturnType, TState, TExtraThunkArg>(
      thunkAction: ThunkAction<TReturnType, TState, TExtraThunkArg, TBasicAction>
    ): TReturnType;
  }

 /// override bindActionCreators
}

@threehams
Copy link
Author

Thanks, I was going to ask if you could open this in a separate PR. I'm not using redux / redux-thunk` much at the moment.

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

Successfully merging this pull request may close these issues.

3 participants