1

我有一个带有 ngrx 状态管理的 Ionic Angular 项目。这是我的状态定义:

export interface State {
  loaded: boolean;
  loading: boolean;
  invoices: InvoicesState;
  invoicePositions: InvoicePositionsState;
  selectedInvoice: InvoiceModel;
  availableTasks: AvailableTasksModel;
  nextPageUrl: string;
  invoicesFilter: string;
  comments: InvoiceCommentsState;
  selectedComment: CommentModel;
}

export interface InvoicesState extends EntityState<InvoicePreviewModel>{}

export const adapterInvoices: EntityAdapter<InvoicePreviewModel> = createEntityAdapter<InvoicePreviewModel>({
  selectId: (invoice: InvoicePreviewModel) => invoice.id,
  sortComparer: false,
});

...

export const initialState: State = {
  loaded: false,
  loading: false,
  invoices: adapterInvoices.getInitialState({}),
  invoicePositions: adapterInvoicePositions.getInitialState({}),
  selectedInvoice: null,
  availableTasks: null,
  nextPageUrl: null,
  invoicesFilter: null,
  comments: adapterInvoiceComments.getInitialState({}),
  selectedComment: null,
};

当满足几个特定条件时,我想对状态进行一些更改。首先,我需要检查具有特定 id 的实体是否存储在EntityState.

这是reducer中发生错误的代码:

  on(InvoicesActions.stateTransitionSuccess, (state, { invoice, invoicePreview }) => {
    let invoices: InvoicesState;

    if (state.invoices.ids.includes(invoicePreview.id)) {
      invoices = adapterInvoices.updateOne({
        id: invoicePreview.id,
        changes: invoicePreview
      }, state.invoices);
    } else {
      // tbd
      // more conditions and logic

    }

    return {
      ...state,
      loaded: true,
      loading: false,
      selectedInvoice: invoice,
      invoices
    }
  }),

includes()函数由于某种原因需要一个never类型参数,我不明白为什么。这是我得到的错误:

Argument of type 'number' is not assignable to parameter of type 'never'.

错误

4

1 回答 1

1

发生这种情况是因为ids类型是number[] | string[],所以您需要number[]在使用它之前将其转换为数字数组,或者您可以使用some而不是includes直接使用,如下所示:

if (state.invoices.ids.some((id) => id === invoicePreview.id))
于 2021-08-19T10:58:02.547 回答