danielroe/typed-vuex

fix: actions and accessorType is untyped when using nuxtServerInit()

avxkim opened this issue · 1 comments

Here's the reproduction link: https://codesandbox.io/s/elated-ives-x1hzb?file=/store/index.ts

Take a look at /store/index.ts file:

image

When i'm trying to use $accessor inside nuxtServerInit() then i'm gettin error about actions and accessorType - "
implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer."

Actually what i'm trying to achieve here, is to fetch data, then commit it:

    async nuxtServerInit(_vuexContext, nuxtContext: Context) {
      const data = await nuxtContext.app.$api.calc.getOffers()
      const offers = data.bundles
      const services = data.services
      const selectedInternet = offers[0].selected
      nuxtContext.app.$accessor.calculator.setInternet(
        offers[0].availableBundles[selectedInternet]
      )
    },

Also what's the purpose of _vuexContext if we're using $accessor?

Just make sure to type the return type of the action that uses the accessor. This should work fine:

    async nuxtServerInit(_vuexContext, nuxtContext: Context): Promise<void> {
      console.log(nuxtContext.app.$accessor.fullEmail);
    }

As for _vuexContext, it's useful for accessing typed versions of commit, state, and getters. Personally I'd only use the accessor within an action to replace dispatch, or if you need to access the root state or another module.