I need assistance in creating a function that can update an object's value based on the provided key and new value while ensuring type safety.
type GroceryStore = {
isOpen: boolean;
offers: string[];
name: string;
};
const myGroceryStore: GroceryStore = {
isOpen: true,
offers: ["banana", "apple", "kiwi"],
name: "FruitMart",
};
// How can we ensure type safety?
const updateGroceryStore = (key: keyof GroceryStore, value: any) => {
myGroceryStore[key] = value;
};
// Should execute correctly
updateGroceryStore("isOpen", true);
// Should trigger TypeScript error
updateGroceryStore("isOpen", "Walmart");
// Should trigger TypeScript error
updateGroceryStore("isOpen", ["orange", "pear", "pineapple"]);