vuex-ts-enhance

vuex types enhance
61,549 11
Install
npm install vuex-ts-enhance
Latest Version:1.0.0
Vue:2
License:MIT
Last Updated:Mar 15, 2024
Links: GitHub  ·  npm
Maintainer: zzwing

vuex-ts-enhance

enhance types from vuex

it will check mapXXXX() params for state, getters, actions, mutations in store

but dispatch is different

const state = {};
const s = new EnhanceStore(state);
export const { dispatch } = s;
dispatch('rootAction')('payload');
dispatch('namespace', 'action')('payload');

Example

state

check for typescript

check for return type

tips from vscode

Usage

use EnhanceStore to create store

import { EnhanceStore } from 'vuex-ts-enhance';
import Vuex from 'vuex';
import Vue from 'vue';
Vue.use(Vuex);

// state cannot be declared
const state = {
  // your state
};
const s = new EnhanceStore(state);
export const {
  mapGetters,
  store,
  mapActions,
  dispatch,
  mapState,
  mapMutations,
} = s;
<template>
  <div>
    {{getName}} {{getUsername}} {{getUsername1}}
  </div>
</template>

<script>
  // @ts-check
  import { mapGetters, mapActions, dispatch } from './store';
  export default {
    computed: {
      ...mapGetters('namespace', ['namespaceGetter']), // will check type
      ...mapGetters(['getter1']),
      ...mapGetters('namespace', {
        getterAlias: 'namespaceGetter',
      }),
    },
    mounted() {
      console.log(this.namespaceGetter);
      console.log(this.getter1);
      console.log(this.getterAlias);
      this.namespaceAction;
      this.action1;
      dispatch('namespace', 'namespaceAction')('payload');
      dispatch('action1')('payload');
    },
    methods: {
      ...mapActions('namespace', ['namespaceAction']),
      ...mapActions(['action1']),
    },
  };
</script>

Note if write in js and your getter or actions is empty, you must to declare it

// store.js
/**
 * @constant
 * @type {import('vuex').GetterTree}
 */
const getters = {};
/**
 * @constant
 * @type {import('vuex').ActionTree}
 */
const actions = {};
/**
 * @constant
 * @type {import('vuex').MutationTree}
 */
const mutations = {};

Notice

  • You can't defined state as StoreOptions
const state: StoreOptions<any> = {}; // don't do that
  • You must be defined context as any if use jsdoc for types
const state = {
  actions: {
    /**
     * @param {any} context
     * @param {string} payload
     */
    someActions(context, payload) {},
  },
};

develop

  • git clone project
  • yarn dev
  • edit dev files
  • add test case

Related Packages

spyfu-vuex-helpers

Utility functions for Vuex.

2,459 9
vuex-pathify

Ridiculously simple Vuex setup + wiring

322,817 1,358
direct-vuex

Use and implement your Vuex store with TypeScript types. Compatible with the Vue...

90,286 255
undo-redo-vuex

A Vuex plugin for module namespaced undo and redo functionality. This plugin tak...

9,207 47
vuex-typescript-fsa

The helper which makes vuex more type-safety

7,911

Version History

Version Released Vue License
1.0.0 ^2.6.11 MIT
0.0.9 ^2.6.11 MIT
0.0.8 ^2.6.11 MIT
0.0.7 ^2.6.11 MIT
0.0.6 ^2.6.11 MIT