【偷懒大法】关于 vue3 + vuex + typescript 没有智能提示,改进办法及使用方式
前景提要:
当前使用的脚手架为 vite 。
其次当前 使用 vue option api 语法
当前解决办法仅限使用了 typescript 构建的框架
javascript 未经测试,可以放到新项目中,配置 package.json 最后打包到私库,在其他项目中进行引入(理论可以)
直接不废话:
首先需要 vuex 的插件,自己去百度安装,不教。
由于 vuex 的官方没有配置 package.json 中关于 types 路径相关内容,导致在常规使用中,所有的函数类型都是 any,所以需要手动增加新的 .d.ts 文件
保证 tsconfig.json 中 compilerOptions.include 包含了接下来的所有文件,接下来的几段代码,对应不通的文件。tips:不建议揉一块,以为我没试过
1 declare module "vuex" { 2 export * from "vuex/types/index.d.ts"; 3 export * from "vuex/types/helpers.d.ts"; 4 export * from "vuex/types/logger.d.ts"; 5 export * from "vuex/types/vue.d.ts"; 6 }
1 declare module "vuex" { 2 export interface Commit<T> { 3 (type: keyof T, payload?: any, options?: CommitOptions): void; 4 <P extends Payload<keyof T>>(payloadWithType: P, options?: CommitOptions): void; 5 } 6 7 export interface Dispatch<T> { 8 (type: keyof T, payload?: any, options?: DispatchOptions): Promise<T[keyof T]>; 9 <P extends Payload<keyof T>>(payloadWithType: P, options?: DispatchOptions): Promise<T[keyof T]>; 10 } 11 12 export interface Payload<T> { 13 type: T; 14 } 15 16 export type UnwrapMethod<T> = T extends (...args: any[]) => () => infer R ? (...args: Parameters<T>) => R : T extends (...args: any[]) => infer R ? R : T; 17 18 export type StoreGetters<T> = { 19 [K in keyof T]: UnwrapMethod<T[K]>; 20 }; 21 22 export interface ActionContext<State, Mutations, Actions, Getters> { 23 dispatch: Dispatch<Actions>; 24 commit: Commit<Mutations>; 25 state: State; 26 getters: StoreGetters<Getters>; 27 rootState: any; 28 rootGetters: any; 29 } 30 31 type ModuleDeepUnwrap<G, K> = { 32 [Module in keyof G]: { 33 [Getter in keyof G[Module][K] as `${string & Module}/${string & Getter}`]: UnwrapMethod<G[Module][K][Getter]>; 34 }; 35 }[keyof G]; 36 37 export type ModuleState<Modules> = { 38 [M in keyof Modules as `${string & M}/${string & keyof Modules[M]["state"]}`]: Modules[M]["state"]; 39 }; 40 41 type ModuleActions<G> = ModuleDeepUnwrap<G, "actions">; 42 43 type ModuleGetters<G> = ModuleDeepUnwrap<G, "getters">; 44 45 export type ModuleMutations<Mutations> = { 46 [M in keyof Mutations as `${string & M}/${string & keyof Mutations[M]["mutations"]}`]: any; 47 }; 48 49 export declare interface Store<State, Mutations, Actions, Getters, Modules extends ActionContext> { 50 readonly state: State & ModuleState<Modules>; 51 readonly getters: StoreGetters<Getters> & ModuleGetters<Modules>; 52 53 dispatch: Dispatch<Actions & ModuleActions<Modules>>; 54 commit: Commit<Mutations & ModuleMutations<Modules>>; 55 56 replaceState(state: State): void; 57 58 subscribe<P extends MutationPayload>(fn: (mutation: P, state: State) => any, options?: SubscribeOptions): () => void; 59 subscribeAction<P extends ActionPayload>(fn: SubscribeActionOptions<P, State>, options?: SubscribeOptions): () => void; 60 watch<T>(getter: (state: State, getters: any) => T, cb: (value: T, oldValue: T) => void, options?: WatchOptions): () => void; 61 62 registerModule<T>(path: string, module: Module<T, State>, options?: ModuleOptions): void; 63 registerModule<T>(path: string[], module: Module<T, State>, options?: ModuleOptions): void; 64 65 unregisterModule(path: string): void; 66 unregisterModule(path: string[]): void; 67 68 hasModule(path: string): boolean; 69 hasModule(path: string[]): boolean; 70 71 hotUpdate(options: { actions?: ActionTree<State, State>; mutations?: MutationTree<State>; getters?: GetterTree<State, State>; modules?: ModuleTree<State> }): void; 72 } 73 }
怎么使用:
众所周知,在创建 vuex 存储对象是基本结构为 modules(模块)、state(存储)、mutations(命令)、actions(过程)和getters(获取),在所有的结构中都不允许为空(因为我没判断),其次在 modules 中 必须开启 namespaced (因为就是按着这个开启洗的)
众所周由知,在 app.use(store) 后会自动创建到 vue 对象内部,即 this.$store, 但是 由于 缺少 描述提示所以,需要增加新的提示
1 import { Store, createStore } from "vuex"; 2 import plugin from "./modules/plugin"; 3 4 const Modules = { plugin }; 5 const State = {}; 6 const Actions = {}; 7 const Mutations = {}; 8 const Getters = {}; 9 10 /// 定义新的类型 11 type StoreType = Store<typeof State, typeof Mutations, typeof Actions, typeof Getters, typeof Modules>; 12 const Store = createStore<any>({ 13 modules: Modules, 14 state: State, 15 mutations: Mutations, 16 actions: Actions, 17 getters: Getters, 18 }) as StoreType; 19 20 21 export default Store as any; 22 23 /// 提供外部的类型 24 declare module "@vue/runtime-core" { 25 export interface ComponentCustomProperties { 26 $store: StoreType; 27 } 28 }
1 export default { 2 namespaced: true, // 关键配置 3 state: State, 4 mutations: Mutations, 5 actions: Actions, 6 getters: Getters, 7 };
好了大概就这样。

浙公网安备 33010602011771号