I have an Ionic Angular project with ngrx State management. Here is my State definition:
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,
};
I want to make some changes to state when couple of specific conditions are met. Firstly I need to check if entity with specific id is stored in EntityState
.
here is the code inside reducer where error occurs:
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()
function expects a never
type parameter for some reason and I don't understand why. Here is the error that I get:
Argument of type 'number' is not assignable to parameter of type 'never'.
That happened because the ids
type is number[] | string[]
, so you need to cast it to a numbers array number[]
before using it, or you can use some
instead of includes
directly like the following:
if (state.invoices.ids.some((id) => id === invoicePreview.id))