npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vuex-ts-decorators-fork-7

v0.0.14

Published

TypeScript decorators for working with Vuex, Initial Copyright - Casey Corcoran

Downloads

5

Readme

⚠️ 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

Primer

While working with decorators in TypeScript, it helps to have a basic understanding of what they are (and aren't) doing. With these decorators we'll write classes which are transformed into Vuex module/store definitions. It's important to note that we will never use new or extend with the decoratated classes.

Utilizing class allows for a straightforward and ergonomic syntax while also providing usable typings down the line. When we combine that benefit with the added convenience of a normalized scope for our actions, mutations and getters (provided by the decorators) we end up with less boilerplate, strict-typing and clearer code across the board.

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: {
    ['myStore/myGetter'](state, getters) {
      return state.prop + ' gotten';
    },
    ['myStore/myOtherGetter'](state, getters) {
      return getters.myGetter + ' again';
    }
  },
  actions: {
    ['myStore/myAction']({commit, getters}, payload) {
      commit('myStore/myMutation', getters.['myStore/myOtherGetter'] + payload.prop);
    }
  },
  mutations: {
    ['myStore/myMutation'](state, payload) {
      state.prop = payload;
    }
  }
})

With Decorators:

@module()
class MyStore {
  prop = 'value';
  @getter('myStore/myGetter')
  get myGetter(): string {
    return this.prop + ' gotten';
  }
  @getter('myStore/myOtherGetter')
  get myOtherGetter(): string {
    return this.myGetter + ' again';
  }
  @action('myStore/myAction')
  private myAction(payload: string): Promise<void> {
    this.myMutation(this.myOtherGetter + payload.prop);
  }
  @mutation('myStore/myMutation')
  private myMutation(payload: string) {
    this.prop = payload;
  }
}

Typing your stores and modules

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

Declaring actions, getters and mutations

Leveraging TypeScript's “declaration merging” we can easily specify our store's api to achieve type-safety and code-completion throughout our application.

In order to define our api, we first import a few constants and declare our module/store's api:

// myStore.ts

import {Actions, Getters, Mutations, Promises, Store} from 'vuex-ts-decorators/constants';

declare module 'vuex-ts-decorators/constants' {
  // name: payload type
  interface Actions {
    'myStore/myAction': string;
  }
  // name: promise type *required*
  interface Promises {
    'myStore/myAction': void;
  }
  // name: payload type
  interface Mutations {
    'myStore/myMutation': string;
  }
  // name: type
  interface Getters {
    'myStore/myGetter': string,
    'myStore/myOtherGetter': string
  }
}

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.