ReduxToolikt 实体适配器问题:如何定义更新的 selectId 类型?
ReduxToolikt entity adapter question: how do i define the selectId type for update?
我正在学习如何在工作示例中使用 createEntityAdapter,但我无法正确解析 selectId
更新功能。
工作沙箱:https://codesandbox.io/s/createentityadapter-demo-forked-5rvl4
实体适配器初始化为:
const entityAdapter = createEntityAdapter<Book>({
selectId: (book) => book.bookId,
sortComparer: (a, b) => a.title.localeCompare(b.title)
});
添加值正常:
const stateOne = entityAdapter.addOne(initialState, {
bookId: 1,
title: "hello"
});
const stateTwo = entityAdapter.addMany(stateOne, [
{ bookId: 2, title: "two" },
{ bookId: 3, title: "three" },
{ bookId: 4, title: "four" }
]);
但是 updateOne 在 bookId 上给出了类型错误:
const stateThree = entityAdapter.updateOne(stateTwo, {
bookId: 3,
changes: {
title: "three - change"
}
});
TS 错误:
(property) bookId: number
No overload matches this call.
Overload 1 of 2, '(state: EntityState, update: Update): EntityState', gave the following error.
Argument of type '{ bookId: number; changes: { title: string; }; }' is not assignable to parameter of type 'Update'.
Object literal may only specify known properties, and 'bookId' does not exist in type 'Update'.
Overload 2 of 2, '(state: EntityState, update: { payload: Update; type: string; }): EntityState', gave the following error.
Argument of type '{ bookId: number; changes: { title: string; }; }' is not assignable to parameter of type '{ payload: Update; type: string; }'.
Object literal may only specify known properties, and 'bookId' does not exist in type '{ payload: Update; type: string; }'.ts(2769)
当我明确定义 bookId
作为我的 select ID 时,为什么我会看到这个错误?
对于 update*
方法,操作负载必须是一个对象,该对象具有完全命名为 id
和 changes
的字段,无论您的数据类型中的实际 ID 字段是什么.所以,这应该可以正常工作:
const stateThree = entityAdapter.updateOne(stateTwo, {
id: 3,
changes: {
title: "three - change"
}
});
我正在学习如何在工作示例中使用 createEntityAdapter,但我无法正确解析 selectId
更新功能。
工作沙箱:https://codesandbox.io/s/createentityadapter-demo-forked-5rvl4
实体适配器初始化为:
const entityAdapter = createEntityAdapter<Book>({
selectId: (book) => book.bookId,
sortComparer: (a, b) => a.title.localeCompare(b.title)
});
添加值正常:
const stateOne = entityAdapter.addOne(initialState, {
bookId: 1,
title: "hello"
});
const stateTwo = entityAdapter.addMany(stateOne, [
{ bookId: 2, title: "two" },
{ bookId: 3, title: "three" },
{ bookId: 4, title: "four" }
]);
但是 updateOne 在 bookId 上给出了类型错误:
const stateThree = entityAdapter.updateOne(stateTwo, {
bookId: 3,
changes: {
title: "three - change"
}
});
TS 错误:
(property) bookId: number No overload matches this call. Overload 1 of 2, '(state: EntityState, update: Update): EntityState', gave the following error. Argument of type '{ bookId: number; changes: { title: string; }; }' is not assignable to parameter of type 'Update'. Object literal may only specify known properties, and 'bookId' does not exist in type 'Update'. Overload 2 of 2, '(state: EntityState, update: { payload: Update; type: string; }): EntityState', gave the following error. Argument of type '{ bookId: number; changes: { title: string; }; }' is not assignable to parameter of type '{ payload: Update; type: string; }'. Object literal may only specify known properties, and 'bookId' does not exist in type '{ payload: Update; type: string; }'.ts(2769)
当我明确定义 bookId
作为我的 select ID 时,为什么我会看到这个错误?
对于 update*
方法,操作负载必须是一个对象,该对象具有完全命名为 id
和 changes
的字段,无论您的数据类型中的实际 ID 字段是什么.所以,这应该可以正常工作:
const stateThree = entityAdapter.updateOne(stateTwo, {
id: 3,
changes: {
title: "three - change"
}
});