ngrx EntityState.ids.includes() 期望类型为 never 的参数
ngrx EntityState.ids.includes() expects parameter with type never
我有一个带有 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'.
发生这种情况是因为 ids
类型是 number[] | string[]
,因此您需要在使用前将其转换为数字数组 number[]
,或者您可以使用 some
而不是像下面这样直接 includes
:
if (state.invoices.ids.some((id) => id === invoicePreview.id))
我有一个带有 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'.
发生这种情况是因为 ids
类型是 number[] | string[]
,因此您需要在使用前将其转换为数字数组 number[]
,或者您可以使用 some
而不是像下面这样直接 includes
:
if (state.invoices.ids.some((id) => id === invoicePreview.id))