'Actions'类型中不存在属性'payload'

Khu*_*shi 6 typescript visual-studio-code ngrx

我是TypeScript和Visual Studio Code的新手.我收到以下错误:

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

我的代码:

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;
Run Code Online (Sandbox Code Playgroud)

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)
      };
    }
}
Run Code Online (Sandbox Code Playgroud)

任何建议都会非常有帮助.

ols*_*lsn 2

您发布的代码看起来不完整,因为在reducer.ts中您正在使用list.ActionTypes.LOAD_SUCCESS,但这不是您发布的action.ts的一部分。

所以一种猜测可能是,您LoadSuccessAction缺少payload-attirbute - 例如public payload: Item

另一个猜测是您忘记将LoadSuccess-type 联合起来:

export type Actions = AddBookAction | LoadSuccessAction;
Run Code Online (Sandbox Code Playgroud)