danielroe/typed-vuex

feat: allow root mutations/actions from module store

Closed this issue ยท 2 comments

๐Ÿ› The bug
In vuex store there is a way to use root mutations/actions from module
https://vuex.vuejs.org/api/#commit

With nuxt-typed-vuex, as I understand, there is no such type in tree, so, when I pass this option and try to run my project, I get an error like

88:16 Argument of type '"rootMutation"' is not assignable to parameter of type '"actionDoSomething"'.

๐Ÿ› ๏ธ To reproduce
Steps to reproduce the behavior:

  1. Add some mutations to root state
// index.ts
export const mutations = mutationTree(state, {
  rootMutation: (state: RootState) => {
    // mutate something
  },
});
  1. Try to use root mutation from module with { root: true } option like
// module.ts
export const actions = actionTree(
  { state, getters, mutations },
  {
    actionDoSomething({ commit }) {
      commit('rootMutation', null, { root: true });
    },
}
  1. Try to run the project
  2. Get an error in log

๐ŸŒˆ Expected behaviour
No errors

@Yolley The reason I've not enabled that is because it would stop the type inference working for local commits (see docs). There is a type-safe way of making root mutations, however. Try:

// module.ts
export const actions = actionTree(
  { state, getters, mutations },
  {
    actionDoSomething() {
      this.app.$accessor.rootMutation()
    },
}

Does that work for you, or does your use case require calling commit?

@Yolley The reason I've not enabled that is because it would stop the type inference working for local commits (see docs). There is a type-safe way of making root mutations, however. Try:

// module.ts
export const actions = actionTree(
  { state, getters, mutations },
  {
    actionDoSomething() {
      this.app.$accessor.rootMutation()
    },
}

Does that work for you, or does your use case require calling commit?

Yes, this way works, thank you!