我在我的应用程序中使用不同类型的“Todo”模型。这些类型是通过一个名为“type”的字段实现的。
const BaseTodo = types
.model("BaseTodo", {
id: types.optional(types.number, () => Math.random()),
title: types.string,
finished: false,
type: types.string
})
.actions(self => ({
toggle() {
self.finished = !self.finished;
},
changeToShoppingType() {
const snap = getSnapshot(self);
console.log("snapshot: ", snap);
const newSnap = {};
Object.assign(newSnap, snap, { type: "shopping" });
console.log("newSnap: ", newSnap);
applySnapshot(self, newSnap);
console.log("after apply snap: ", self);
}
}));
const ShoppingTodo = BaseTodo.named("ShoppingTodo").props({
type: types.literal("shopping")
});
const CleaningTodo = BaseTodo.named("CleaningTodo").props({
type: types.literal("cleaning")
});
export const Todo = types.union(ShoppingTodo, CleaningTodo);
export const TodoStore = types
.model("TodoStore", {
todos: types.array(Todo)
});
我的问题:当我想通过对我的对象应用快照将我的步骤类型从“清洁”更改为“购物”时,我收到错误消息:
...在路径“/type”值“shopping”不可分配给类型:“cleaning”
任何提示如何实现我的用例?
我创建了一个代码框示例,说明我想如何更改类型(只需单击按钮): https ://dm71z.csb.app/
不幸的是,我无法在控制台中显示错误。