Search code examples
typescriptvue.jspinia

Property '**' does not exist on type '**' in pinia store


I have this pinia store in my Vue app :

export const useUserStore = defineStore('user', {
  state: () => {
    return {
      user: null as User | null
    }
  },
  actions: {
    setUser(user: User | null) {
      this.user = user;
    }
  }
});

and I get this error :

 [vue-tsc] Property 'user' does not exist on type '{ setUser(user: User | null): void; }'. 

I really don't understand what is the issue with my typings


Solution

  • You should be able to fix this by not using the object literal configuration and instead using the setup function:

    import { ref } from 'vue'
    import { defineStore } from 'pinia'
    
    export const useUserStore = defineStore('user', () => {
      const user = ref<User | null>(null)
      const setUser = (userUpdate: User) => { user.value = userUpdate }
    
      return { user, setUser }
    })
    

    I didn't run the code and I'm not a Vue expert so it may not be 100% what you want but should get you in the right ballpark.