/src/middlewares/auth.ts file:
import store from '@/store'
export default {
guest(): void {
if (store.state.auth.authenticated === false) {
// do some action
}
}
}
/src/store.ts file:
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
modules: {
auth
}
})
/src/store/auth.ts:
import { Module, VuexModule, Mutation, Action } from 'vuex-module-decorators'
@Module
export default class Auth extends VuexModule {
public authenticated: boolean = false
}
However i'm getting TS error: Object is of type 'unknown'
Then my npm run build
fails. How can i type store
so this error would disappear?
UPDATE 1
Problems is in line: if (store.state.auth.authenticated === false)
UPDATE 2
My directory structure for these 3 files:
- /src/middlewares/auth.ts
- /src/store.ts
- /src/store/auth.ts
UPDATE 3
if i change store.state.auth.authenticated
to store.state
then compiler stops complaining, i think its related to my store auth.ts file, i need to type definition it somehow.
store
directory orstore.js
file that might be getting loaded in place ofstore.ts
– Umbelliferousauth
module – Libaustore.ts
file but you're right, it is suspiciously missing from the code above – Umbelliferousimport store from '@/store.ts'
. It's best to not make your build system guess betweensrc/store.ts
(the file) andsrc/store
(the directory) – Umbelliferousimport store from '@/store.ts' ... const state = store.state as any;
. I wouldn't go so far as calling this a clean solution, but at least my code compiles now. – Mayman