All Projects → snaptopixel → Vuex Ts Decorators

snaptopixel / Vuex Ts Decorators

Write Vuex stores and modules with type-safety and code completion

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Vuex Ts Decorators

Vue Template With Element Ui
A Vue.js template with element UI.
Stars: ✭ 42 (-20.75%)
Mutual labels:  vuex
Moleculer Decorators
decorators for moleculer
Stars: ✭ 47 (-11.32%)
Mutual labels:  decorators
Vuestic Admin
Free and Beautiful Vue 3 Admin Template
Stars: ✭ 8,398 (+15745.28%)
Mutual labels:  vuex
Vue Firebase Starter
boilerplate of vue/vuex/vue(x)-router, with sass/prerendering, muse-ui, and firebase/firebaseui
Stars: ✭ 43 (-18.87%)
Mutual labels:  vuex
Vue Spa
vue-spa : vue + vue-router + axios + vuex + vux 快速成型移动端项目,直接使用。欢迎star
Stars: ✭ 46 (-13.21%)
Mutual labels:  vuex
Duix
A State Manager focused on KISS and Pareto's Principle
Stars: ✭ 48 (-9.43%)
Mutual labels:  vuex
Feeds App Fe
Stars: ✭ 40 (-24.53%)
Mutual labels:  vuex
Lihkg Web
LIHKG Web client (Unofficial)
Stars: ✭ 52 (-1.89%)
Mutual labels:  vuex
Redux Connect Decorator
React Redux's connect as a decorator.
Stars: ✭ 46 (-13.21%)
Mutual labels:  decorators
Ant Design Vue Pro
👨🏻‍💻👩🏻‍💻 Use Ant Design Vue like a Pro!
Stars: ✭ 8,965 (+16815.09%)
Mutual labels:  vuex
Quasar Cloud Demo
Demo Project using Quasar & VueJs. Shows Vuex,Vuelidate,Axios etc...
Stars: ✭ 44 (-16.98%)
Mutual labels:  vuex
Vuetify Material Dashboard
Vuetify Material Dashboard - Open Source Material Design Admin
Stars: ✭ 1,023 (+1830.19%)
Mutual labels:  vuex
Ticket Conductor
A free and open-source Laravel 5.5 and VueJS (SPA) Ticket system
Stars: ✭ 48 (-9.43%)
Mutual labels:  vuex
Ember Arg Types
Runtime type checking & defaulting for glimmer component arguments powered by prop-types & decorators
Stars: ✭ 43 (-18.87%)
Mutual labels:  decorators
Vue Pomo
A progressive web app for the Pomodoro Technique, built with Vue 2.0, Vuex and Firebase.
Stars: ✭ 51 (-3.77%)
Mutual labels:  vuex
Vue Cli3.0 Vueadmin
基于vue-cli3.0+vue+elementUI+vuex+axios+权限管理的后台管理系统
Stars: ✭ 1,002 (+1790.57%)
Mutual labels:  vuex
Vue Admin Pro
Solution for company middle and backstage, written using the vue and nuxtjs
Stars: ✭ 48 (-9.43%)
Mutual labels:  vuex
Koa Vue Ssr Template
This template built with vue 2.x, vue-router & vuex & webpack3 with server-side rendering by koa
Stars: ✭ 53 (+0%)
Mutual labels:  vuex
Custom Elements Ts
Create native custom elements using Typescript
Stars: ✭ 52 (-1.89%)
Mutual labels:  decorators
Parcel Vue Ts
📦 Boilerplate for Vue.js & Typescript, base on Parcel bundler.
Stars: ✭ 49 (-7.55%)
Mutual labels:  vuex

⚠️ This project is currently undergoing refactoring and is NOT production ready ⚠️

TypeScript Decorators for Vuex Build Status npm package

Write Vuex stores and modules with type-safety and code completion

Quick primer

Decorators can seem quite magical so it helps to have a basic understanding of what they do (and don't do). In this implementation the main job of decorators is to transform a class definition into a “shape” which Vuex supports.

So, you write a nice class with comfortable syntax and the decorators do the legwork of mapping, transforming and replacing your class with something else. They also normalize the scope in which your actions, mutations and getters operate so instead of using function params you end up just using this to access things in a straightforward (and type-safe) way.

Inspiration

This solution is heavily inspired by the excellent work on vue-class-component which makes writing components in TypeScript very ergonomic and fun. The goal of this project is to apply similar patterns to Vuex while also providing (and allowing for) TypeScript niceties like code-completion and type-safety all the way down.

Basic example

The following snippet shows a standard Vuex declaration followed by an example using decorators.

// Without decorators
const MyStore = new Vuex.Store({
  state: {
    prop: 'value'
  },
  getters: {
    myGetter(state, getters) {
      return state.prop + ' gotten';
    },
    myOtherGetter(state, getters) {
      return getters.myGetter + ' again';
    }
  },
  actions: {
    myAction({commit, getters}, payload) {
      commit('myMutation', getters.myOtherGetter + payload.prop);
    }
  },
  mutations: {
    myMutation(state, payload) {
      state.prop = payload;
    }
  }
})

// With decorators
@module({
  store: true
})
class MyStore {
  prop = 'value';
  get myGetter(): string {
    return this.prop + ' gotten';
  }
  get myOtherGetter(): string {
    return this.myGetter + ' again';
  }
  @action
  myAction(payload) {
    this.commit('myMutation', this.myOtherGetter + payload.prop);
  }
  @mutation
  myMutation(payload) {
    this.prop = payload;
  }
}

Conventions for type-safety

You may have noticed a problem with the second example above. Inside myAction we're making a call to this.commit() which is not defined on the class and will throw an error at compile time.

It's important to note that by themselves, these decorators do not provide full type-safety for Vuex. Instead they allow you to write your stores and modules in a way that allows you to achieve type-safety via normal TypeScript conventions.

Example store with typed dispatch and commit

type actions = {
  myAction: {prop: string}
}

type mutations = {
  myMutation: string
}

type TypedDispatch = <T extends keyof actions>(type: T, value?: actions[T] ) => Promise<any[]>;
type TypedCommit = <T extends keyof mutations>(type: T, value?: mutations[T] ) => void;

@module({
  store: true
})
class MyStore {
  dispatch: TypedDispatch;
  commit: TypedCommit;
  prop = 'value';
  get myGetter(): string {
    return this.prop + ' gotten';
  }
  get myOtherGetter(): string {
    return this.myGetter + ' again';
  }
  @action
  myAction(payload: actions['myAction']) {
    this.commit('myMutation', this.myOtherGetter + payload.prop);
  }
  @mutation
  myMutation(payload: mutations['myMutation']) {
    this.prop = payload;
  }
}

Example usage and code structure

For futher answers and information, please check out the companion vuex-ts-example project. You'll be able to see the decorators in action as well as some guidance on how you can structure your code for the best results.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].