Reducer,实体适配器:属性 'accounts' 在类型上不存在 - Angular 7,Rxjs 6,Ngrx

Reducer, entity adapter: Property 'accounts' does not exist on type - Angular 7, Rxjs 6, Ngrx

我收到错误:

ERROR in src/app/account/account.reducers.ts(24,37): error TS2339: Property 'accounts' does not exist on type '{ account: Account; } | { accounts: Account[]; }'.
  Property 'accounts' does not exist on type '{ account: Account; }'.

这是指减速器中适配器上的addAll行:

export interface AccountState extends EntityState<Account> {
  allAccountsLoaded : boolean;
}

export const adapter : EntityAdapter<Account> = createEntityAdapter<Account>();

export const initialAccountState: AccountState = adapter.getInitialState({
  allAccountsLoaded: false
});

export function accountReducer(state = initialAccountState, action: AccountActions): AccountState {
  switch(action.type) {
    case AccountActionTypes.AccountLoaded:
      adapter.addOne(action.payload.account, state);

    case AccountActionTypes.AllAccountsLoaded:
      adapter.addAll(action.payload.accounts, {...state, allAccountsLoaded: true});
    default: {
      return state;
    }
  }
}

但是当我查看 reducer 的相关操作时,它们传递的有效负载是一个帐户数组,其 prop 名称为 "accounts"

export class AllAccountsLoaded implements Action {
  readonly type = AccountActionTypes.AllAccountsLoaded;

  constructor(public payload: {accounts: Account[]}) {
  }
}

所以似乎应该正确传递有效负载。让我担心的是错误的一部分:'{ account: Account; } | { 账户:账户[]; }'。我已经看到 Ngrx 抛出那种错误,如果我在 effects observable 中输入错误更改其中一个事件名称,但我已经检查过它,第一眼看起来不错:

  @Effect()
  loadAllAccounts$ = this.actions$
  .pipe(
    ofType<AllAccountsRequested>(AccountActionTypes.AllAccountsRequested),
    withLatestFrom(this.store.pipe(select(allAccountsLoaded))),
    filter(([action, allAccountsLoaded]) => !allAccountsLoaded),
    mergeMap(() => this.accountService.getUserAccounts()),
    map(accounts => new AllAccountsLoaded({accounts}))
  );

好的。如果其他人可能 运行 进入这个。该错误消息非常具有误导性。如果你看一下 reducer 定义:

export function accountReducer(state = initialAccountState, action: AccountActions): AccountState {
  switch(action.type) {
    case AccountActionTypes.AccountLoaded:
      adapter.addOne(action.payload.account, state);

    case AccountActionTypes.AllAccountsLoaded:
      adapter.addAll(action.payload.accounts, {...state, allAccountsLoaded: true});
    default: {
      return state;
    }
  }
}

您会注意到在适配器存储库样式函数附近缺少一个 return 子句,因此它应该是:

return adapter.addAll(action.payload.accounts, {...state, allAccountsLoaded: true});

这只是一个打字错误,但您可能需要一段时间才能找到,因为该错误将您指向完全不同的方向。