I am having issues trying to get Vuex with Typescript working. I did an example without typescript which worked just fine as shown first below. I am creating a Vuex module and then adding it as shown below
src -> store -> index.js
import Vue from 'vue';
import Vuex from 'vuex';
import robotsModule from './modules/products';
Vue.use(Vuex);
export default new Vuex.Store({
modules: {
products: productsModule
},
});
src -> store -> modules -> products.js
import axios from 'axios';
export default {
namespaced: true,
state: {
products: [],
},
mutations: {
addProduct(state, product) {
state.products.push(product);
},
updateProduct(state, products) {
state.parts = products;
},
},
actions: {
getProducts({ commit }) {
axios
.get('/api/products')
.then(result => commit('updateProduct', result.data))
.catch(console.error);
}
},
};
Now when I want to use typescript it keeps complaining that
"Binding element 'commit' implicitly has an 'any' type."
As you can see I have currently specified the state as any which also seems wrong. How do I make this work nicely with typescript?
src -> store -> modules -> products.ts
import axios from 'axios';
import { Product } from '@/models/product';
export default {
namespaced: true,
state: {
products: new Array<Product>(),
},
mutations: {
updateProducts(state: any, products: Product[]) {
state.products = products;
},
addProduct(state: any, product: Product) {
state.products.push(product);
},
},
actions: {
getProducts({ commit }) {
axios
.get('/api/products')
.then(result => commit('updateProducts', result.data))
.catch(console.error);
},
},
};