Skip to content

Typescript errors trying to index my object #1692

Answered by EmilTholin
alex-hladun asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @alex-hladun!

You have to access the map properties with the get method. You also have to make sure you handle the potential undefined values, or TypeScript will complain. For example:

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 toggleOpen action directly on the StoreHours mo…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by alex-hladun
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants