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

createReducer handle type/action not giving same action type #222

Open Baterka opened 4 years ago

Baterka commented 4 years ago

Description

When creating reducer by createReducer<State, Action> you not get same action type when using handleType and handleAction if your action was created by createAsyncAction.

Mandatory info

How to Reproduce

export interface IUser {
  id: number;
  email: string;
  full_name: string;
  data: {};
  roles: [];
}

export enum EActionTypes {
  FETCH_PROFILE_REQUEST = '@@auth/FETCH_PROFILE_REQUEST',
  FETCH_PROFILE_SUCCESS = '@@auth/FETCH_PROFILE_SUCCESS',
  FETCH_PROFILE_FAILURE = '@@auth/FETCH_PROFILE_FAILURE',
}

export const fetchProfileAsync = createAsyncAction(
  EActionTypes.FETCH_PROFILE_REQUEST,
  EActionTypes.FETCH_PROFILE_SUCCESS,
  EActionTypes.FETCH_PROFILE_FAILURE
)<undefined, IUser, Error>();

Create reducer like so and you get type error:

const reducer = createReducer<IState, RootAction>(initialState).handleAction(
  fetchProfileAsync.success,
  (state, action) => {
    return {
      ...state,
      profile: action.payload,
    };
  }
);

You get action type: (parameter) action: { type: EActionTypes.FETCH_PROFILE_SUCCESS; payload: {}; } | PayloadAction<EActionTypes.FETCH_PROFILE_SUCCESS, IUser> and because that you get type error.

Create reducer like so and everything is ok:

const reducer = createReducer<IState, RootAction>(initialState).handleType(
  EActionTypes.FETCH_PROFILE_SUCCESS,
  (state, action) => {
    return {
      ...state,
      profile: action.payload,
    };
  }
);

Expected behavior

action should be type: (parameter) action: PayloadAction<EActionTypes.FETCH_PROFILE_SUCCESS, IUser>

Suggested solution(s)

Project Dependencies



## Environment (optional)
<!-- Fill if you think it's relevant to your issue -->
- OS: Win10
- Node Version: 12

[1]: https://github.com/piotrwitek/typesafe-actions#compatibility-notes
[2]: https://github.com/piotrwitek/typesafe-actions#migration-guides
C-Higgins commented 4 years ago

Your RootAction type is wrong