Typescript errors trying to index my object #1692
-
Hey all, back again with another question. I am running into Typescript issues when adding an action to change my data and haven't been able to find a solution. I am getting compile error
when i try to use the following action:
The issue is using storeNumber as an index. Here is my model:
and my types:
for Reference, here is my data:
I thought adding the types.identifer might work, but no luck. Any help is greatly appreciated :) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @alex-hladun! You have to access the map properties with the const Profile = types
.model({
stores: types.optional(types.map(StoreType), {})
})
.actions((self) => ({
toggleOpen(storeNumber: string, day: string) {
const store = self.stores?.get(storeNumber);
if (store) {
const storeHour = store.storeHours.get(day);
if (storeHour) {
storeHour.open = false;
}
}
}
})); I'm not exactly sure what your use case is, but it might be easier to just put the const StoreHours = types
.model({
open: types.boolean
})
.actions((self) => ({
toggleOpen() {
self.open = !self.open;
}
})); |
Beta Was this translation helpful? Give feedback.
Hi @alex-hladun!
You have to access the map properties with the
get
method. You also have to make sure you handle the potentialundefined
values, or TypeScript will complain. For example:I'm not exactly sure what your use case is, but it might be easier to just put the
toggleOpen
action directly on theStoreHours
mo…