1.3.0 • Published 5 years ago

vuex-typed v1.3.0

Weekly downloads
5
License
MIT
Repository
github
Last release
5 years ago

vuex-typed

Vue>=2.5 Vuex>=3.0 TypeScript>=2.8 License: MIT

Vuex can be strongly-typed by interface.
And you can use this with a very simple declaration.

Unlike existing projects, you can also strongly-typed the promise returned from dispatch.
This is an implementation for when the \$store on vue can now be typed in the future.

Installation

npm install --save-dev vuex-typed

Example

/*
 * Attention that it is annotated according to the type declared by the interface.
 */

import Vue from 'vue';
import Vuex from 'vuex';
import { ActionTree, GetterTree, MutationTree } from 'vuex-typed';

Vue.use(Vuex);

/*
 * State
 */

export interface IState {
  date: Date;
}

const state: IState = {
  date: new Date()
};

/*
 * Getters
 */

interface IGetters {
  time: number;
}

const getters: GetterTree<IGetters, IState> = {
  time: state => state.date.getTime()
};

/*
 * Mutations
 */

interface IMutations {
  setDate: Date;
}

const mutations: MutationTree<IMutations, IState> = {
  setDate: (state, payload) => {
    state.date = payload;
  }
};

/*
 * Actions
 */

interface IActions {
  set: (payload: Date) => void;
  diff: (payload: Date) => number;
  now: () => Promise<number>;
}

const actions: ActionTree<IActions, IState, IMutations, IGetters> = {
  set: ({ commit }, payload) => {
    commit('setDate', payload);
  },
  diff: ({ getters }, payload) => {
    return payload.getTime() - getters.time;
  },
  now: () => {
    return new Promise(resolve =>
      setTimeout(() => {
        resolve(new Date().getTime());
      }, 1000)
    );
  }
};

/*
 * Export
 */

export default new Vuex.Store({
  state,
  getters,
  mutations,
  actions
});

LICENSE

MIT

1.3.0

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago