Skip to content

Latest commit

 

History

History
102 lines (68 loc) · 3.22 KB

README.md

File metadata and controls

102 lines (68 loc) · 3.22 KB

vuex-ts-prompt

vuex-ts-prompt is a tool for vuex and TypeScript 4.1+。

thanks

Start

npm install typescript --save-dev
npm i vuex-ts-prompt --save

then modify your store.ts

import Vuex from 'vuex';
import { GetActionsType, GetGettersType, GetMutationsType, GetPayLoad, GetReturnType, GetStateType } from 'vuex-ts-prompt';

const vuexOptions = {
    state,
    getters,
    actions,
    mutations,
    modules: {
        home,
        detail,
    }
};

export type State = GetStateType<typeof vuexOptions>

export type Getters = GetGettersType<typeof vuexOptions>

type Mutations = GetMutationsType<typeof vuexOptions>;

type Actions = GetActionsType<typeof vuexOptions>;

// state?: S | (() => S);
// getters?: GetterTree<S, S>;
// 这里把 state 类型设置为 any 是为了让参数不受约束
const store = new Vuex.Store<any>(vuexOptions)

then add folder types and add file store.d.ts

import { Commit, Dispatch, Getters, State } from '@/store'

declare module 'vue/types/vue' {
	export declare class Store<S> {
		state: State
		getters: Getters
		dispatch: Dispatch
		commit: Commit
	}

	interface Vue {
		$store: Store<State>
	}
}

Reading

typescript对vuex的全支持

使用截图

  • 全局变量 $store

    image-20210125121827015

  • state

    image-20210125121850658

    image-20210125122153256

  • getters

    image-20210125122249448

    image-20210125122330124

    image-20210125122422204

  • commit

    image-20210125122538834

    image-20210125122616232

    image-20210125122648042

  • dispatch

    image-20210125122737544

    image-20210125122803347

    image-20210125122834526