The payload property does not exist in the Actions type

I am new to TypeScript and Visual Studio Code. I get the following error:

*[ts] Property 'payload' does not exist on type 'Actions'.

My code is:

file action.ts:

  import { Action } from '@ngrx/store';
  import { Item } from '../models/list';

  export class AddBookAction implements Action {
      type = ActionTypes.ADD_BOOK;

      constructor(public payload: Item) { }
  }

  export type Actions = AddBookAction;

reducer.ts

import * as list from 'action';

export function reducer(state = initialState, action: list.Actions): State {

switch (action.type) {
    case list.ActionTypes.LOAD: {
      return Object.assign({}, state, {
        loading: true
      });
    }

    case list.ActionTypes.LOAD_SUCCESS: {
      const books = action.payload // Error here

      return {
        loaded: true,
        loading: false,
        ids: books.map(book => book.id)
      };
    }
}

Any advice would be very helpful.

+4
source share
1 answer

The code you posted looks incomplete because you are using the reducer.ts file list.ActionTypes.LOAD_SUCCESS, however this is not part of the action.ts you submitted.

So, we can assume that in yours LoadSuccessActionyou lack payload-attirbute - for example. public payload: Item.

Another hunch is that you forgot to combine LoadSuccess-type in:

export type Actions = AddBookAction | LoadSuccessAction;
+2
source

Source: https://habr.com/ru/post/1661786/


All Articles