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

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

Open
Baterka opened this issue Jan 25, 2020 · 1 comment
Open

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

Baterka opened this issue Jan 25, 2020 · 1 comment

Comments

@Baterka
Copy link

Baterka commented Jan 25, 2020

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
@C-Higgins
Copy link

Your RootAction type is wrong

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

2 participants