Search code examples
vue.jsvuexvuejs3

Object literal may only specify known properties, and ''ALTERA_PROJETO'' does not exist in type 'StoreOptions<Estado>'


I have a project in Vue 3.2, using TypeScript 4.5, and Vuex 4, I'm trying to create a new mutation that edits existing projects, but when I try to create her, I`m gettin this error:

ERROR in src/store/index.ts:24:3
TS2345: Argument of type '{ state: { projetos: never[]; }; mutations: { ADICIONA_PROJETO(state: Estado, nomeDoProjeto: string): void; }; ALTERA_PROJETO(state: Estado, projeto: IProjeto): void; }' is not assignable to parameter of type 'StoreOptions<Estado>'.
  Object literal may only specify known properties, and ''ALTERA_PROJETO'' does not exist in type 'StoreOptions<Estado>'.
    22 |     }
    23 |   },
  > 24 |   'ALTERA_PROJETO' (state: Estado, projeto: IProjeto) {
       |   ^^^^^^^^^^^^^^^^
    25 |       const index = state.projetos.findIndex(proj => proj.id == projeto.id)
    26 |       state.projetos[index] = projeto
    27 |   }

My store/index.ts:

import IProjeto from "@/interfaces/IProjetos";
import { InjectionKey } from "vue";
import { createStore, Store, useStore as vuexUseStore } from "vuex";

interface Estado {
  projetos: IProjeto[]
}

export const key: InjectionKey<Store<Estado>> = Symbol()

export const store = createStore<Estado>({
  state: {
    projetos: []
  },
  mutations: {
    "ADICIONA_PROJETO"(state, nomeDoProjeto: string) {
      const projeto = {
        id: new Date().toISOString(),
        nome: nomeDoProjeto
      } as IProjeto
      state.projetos.push(projeto)
    }
  },
  'ALTERA_PROJETO' (state: Estado, projeto: IProjeto) {
      const index = state.projetos.findIndex(proj => proj.id == projeto.id)
      state.projetos[index] = projeto
  }
})

export function useStore(): Store<Estado> {
  return vuexUseStore(key)
}

And my interfaces/IProjetos:

export default interface IProjeto {
  id: string,
  nome: string
}

and I have no idea why this error is occurring, its a class project and my instructor is not having this error.


Solution

  • You're closing mutations before ALTERA_PROJECTO.

    This:

      mutations: {
        "ADICIONA_PROJETO"(state, nomeDoProjeto: string) {
          const projeto = {
            id: new Date().toISOString(),
            nome: nomeDoProjeto
          } as IProjeto
          state.projetos.push(projeto)
        }
      }, /* 👈 here */
      'ALTERA_PROJETO' (state: Estado, projeto: IProjeto) {
          const index = state.projetos.findIndex(proj => proj.id == projeto.id)
          state.projetos[index] = projeto
      }
    

    should be:

      mutations: {
        ADICIONA_PROJETO(state, nomeDoProjeto: string) {
          const projeto = {
            id: new Date().toISOString(),
            nome: nomeDoProjeto,
          } as IProjeto;
          state.projetos.push(projeto);
        },
        ALTERA_PROJETO(state: Estado, projeto: IProjeto) {
          const index = state.projetos.findIndex((proj) => proj.id == projeto.id);
          state.projetos[index] = projeto;
        },
      },