piotrwitek / typesafe-actions

Typesafe utilities for "action-creators" in Redux / Flux Architecture
https://codesandbox.io/s/github/piotrwitek/typesafe-actions/tree/master/codesandbox
MIT License
2.41k stars 99 forks source link

Can I use createAction with a static meta in a way that createReducer.handleAction allows? #234

Open dimitropoulos opened 4 years ago

dimitropoulos commented 4 years ago

I am looking at using redux-beacon to integrate analytics from the point of my redux store. To do this I need to add meta to some actions. So I basically have typical createAction that looks like this, let's say:

export const setExpertModeAction = createAction('config/SET_EXPERT_MODE')<boolean>();

I'm using this with createReducer like

export const configReducer = createReducer(initialState)
  .handleAction(setExpertModeAction, (state, action) => ({
    ...state,
    expertMode: action.payload
  });

And in my code I just have

dispatch(setExpertModeAction(true))

My problem is I want to, at the level of the creation of the action, create meta for the analytics.

I was able to mock this out as follows:

export interface ActionMetaType {
  analytics?: {
    eventName: string;
    otherEventAttr?: number;
  };
}

export const setExpertModeAction = <T extends boolean>(payload: T) => (
  createAction('config/SET_EXPERT_MODE')<T, ActionMetaType>()(
    payload,
    {
      analytics: {
        eventName: payload ? 'enabled expert mode' : 'disabled expert mode',
      },
    },
  )
);

That way, when I dispatch the action it doesn't need the meta to be there, just like before. My issue is, though, that when I run this I get:

Error: Argument 1 is invalid, it should be an action-creator instance from "typesafe-actions" or action type of type: string | symbol

Any suggestions?