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

Typings for dispatch props #51

Closed
sample-usr opened this issue Jan 8, 2018 · 4 comments
Closed

Typings for dispatch props #51

sample-usr opened this issue Jan 8, 2018 · 4 comments

Comments

@sample-usr
Copy link

Hi,
I'm trying to provide typings for my actions inside a connected component.

interface IDispatchProps {
  setName: typeof setName // where setName is an imported action
}

Given that the action is defined as:

export const setName = actionCreator<string>('SET_NAME');

The idea is to use those typings when defining mapDispatchToProps:

const mapDispatchToProps = (dispatch): IDispatchProps => ({
  setName: (name:string) =>dispatch(setName(name))
})

However I get an error about the typings being incompatible. Is there a way to get the type of that action?

@aikoven
Copy link
Owner

aikoven commented Jan 11, 2018

What exact error do you get?

It's currently not possible to get the type of action from action creator but I think it's possible to add such feature.

@jlmart88
Copy link

I also ran into this problem of wanting to use the type of the action from the action creator. I came up with a solution that "wraps" the action creator factory with another interface that exposes the actions as properties alongside the action creators. This allows for code that looks like:

const authActionCreatorFactory = actionAndActionCreatorFactory('auth/');

const actionsAndActionCreators = {
  refreshToken: authActionCreatorFactory<{token: string}>('REFRESH_TOKEN'),
};

let refreshTokenAction: typeof actionsAndActionCreators.refreshToken.actionType;
refreshTokenAction = actionsAndActionCreators.refreshToken.actionCreator({token: 'new_token'});

Similarly, it exposes the types of each of the three async actions from an async action creator as separate properties (.actionTypes.done, etc). The api could probably be improved a bit (e.g. by exposing the actionType as a property of the action creator, rather than having the action creator and actionType be two separate properties), but I can make a PR with this code if this idea matches what you were thinking @aikoven.

@aikoven
Copy link
Owner

aikoven commented Feb 20, 2018

See microsoft/TypeScript#21847, very soon we'll have a built-in ability to extract a return type of a function.

@aikoven
Copy link
Owner

aikoven commented Jul 2, 2018

So since TypeScript 2.8 you can use the built-in ReturnType type!

Please feel free to reopen if that doesn't solve your problem.

@aikoven aikoven closed this as completed Jul 2, 2018
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

No branches or pull requests

3 participants