Alex l.
Alex l.

Reputation: 273

Typing $store property in Vue components

Here is a recipe on how to type $store property. But it doesn't work for me.

In vue 2 project created with vue-cliI, I added vuex.d.ts file to ./src but I $store property in my components still has type Store<any>

Content of vuex.d.ts file:

//imported from another file
interface State { 
  // ... type of my state
}

declare module '@vue/runtime-core' {

  interface ComponentCustomProperties {
    $store: Store<State>
  }
}

Not sure what information you might need to determine why it's not working so please ask in the comments.

Upvotes: 0

Views: 274

Answers (1)

Ribas Developer
Ribas Developer

Reputation: 62

Adding the vuex.d.ts file is the Vue 3 way of doig this.In Vue 2 there is no official way of adding types to Vuex, so I recommend using a 3rd party lib for this purpose. Take a look at Vuex Typex

Upvotes: 1

Related Questions