Deriving a partial type for updating, a solution
Well what do we do? We can't do this:
type ItemsState = {
// …
update: (id: string, updates: Item) => void;
// …
};
I immediately get yelled at in Item.tsx
.
In fairness, it's not wrong. That's only a part of an Item
.
It's time for a utility type!
Using Partial<T>
type ItemsState = {
items: Item[];
packedItems: Item[];
unpackedItems: Item[];
add: (name: string) => void;
remove: (id: string) => void;
update: (id: string, updates: Partial<Item>) => void;
markAllAsUnpacked: () => void;
};
In the implementation:
const update = (id: string, updates: Partial<Item>) => {
setItems(updateItem(items, id, updates));
};