Using Vuex in Typescript

vuex uses module, and then wants to use store.state.a to report type errors in the TS file

// store/index.ts

// ...
Vue.use(Vuex);

const store: StoreOptions<RootState> = {
    modules: {
        a
    }
};

export default new Vuex.Store<RootState>(store);
// a.ts
// ...
export const state: State = {
    id: 1
};

const namespaced: boolean = true;

export const user: Module<UserState, RootState> = {
    namespaced,
    state
};
// use.ts
// TSVue
store.state.a.id // [ts] "{}" "a"  [2339]

how should I correct this so that this exception is not thrown?
Thank you!

Jun.06,2022

uses Getters, refer to ide-router-ts" rel=" nofollow noreferrer "> https://stackoverflow.com/que...


write in the original vue way is easy to cause type loss, it is recommended to use a vuex ts package to write, my current project is using: vuex-module-decorators , or very easy to use!


define RootState

//storeModel.ts
export interface RootState {
    a:{
        id: string
    }
}

and then introduce

into use.ts
Menu