Home > other >  TS2322: Type '(state: State, exRep: number, exName: string) => void' is not assignable
TS2322: Type '(state: State, exRep: number, exName: string) => void' is not assignable

Time:01-14

I'm currently working with vuex and typescript but I have this error in my code and I have no idea on how to fix it. The error : TS2322: Type '(state: State, exRep: number, exName: string) => void' is not assignable to type 'Mutation '

My code :

import { createStore } from "vuex";
import {State} from "./Types";

const store = createStore<State>({
    state() {
        return {

            exercices: []

        }
    },
    mutations: {
        addEx(state: State, exRep: number, exName: string) {
            const exId = checkid()
            state.exercices.push({ "rep": exRep, "name": exName, "id": exId })
        },
        removeEx(state: State, exoId: Number) {
            console.log(exoId)
            state.exercices = state.exercices.filter(obj => obj.id !== exoId)
        }
    }
})
export default store

The error is at "addEx" mutation

CodePudding user response:

Mutation functions have only 2 parameters, it would be impractical for them to be variadic. The second parameter is optional payload, in case of multiple parameters it should be an object:

addEx(state: State, { exRep, exName }: { exRep: number, exName: string }) {
  ...
  • Related