我有一个带有 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'.