typesafe-actions icon indicating copy to clipboard operation
typesafe-actions copied to clipboard

createReducer handle type/action not giving same action type

Open Baterka opened this issue 5 years ago • 1 comments

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

  • Typesafe-Actions Version: latest
  • TypeScript Version: latest
  • tsconfig.json:
{
  "compilerOptions": {
    "target": "esnext",                       
    "module": "commonjs",                

    "jsx": "react-native",                 

    "noEmit": false,                         

    "strict": true,                  

    "noUnusedLocals": true,     
    "noUnusedParameters": true,           

    "moduleResolution": "node",       

    "esModuleInterop": true,  

    "forceConsistentCasingInFileNames": true,

    "skipLibCheck": true,
    "resolveJsonModule": true
  },
  "exclude": ["node_modules"]
}

Environment (optional)

  • OS: Win10
  • Node Version: 12

Baterka avatar Jan 25 '20 15:01 Baterka

Your RootAction type is wrong

C-Higgins avatar Mar 10 '20 18:03 C-Higgins