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-tstore

v1.1.0

Published

Provides a low-overhead TypeScript wrapper around Vuex that can trigger compilation errors and IntelliSense tips.

Downloads

2,308

Readme

Vuex TypeScript Store

Vuex TStore is a low-overhead TypeScript wrapper around Vuex that can be used to trigger compilation errors and IntelliSense tips in Visual Studio Code.

One of the problems with integrating Vuex with TypeScript is that it can be difficult to track payload types in Vuex. This class generates type-aware bindings to a wrapped Vuex store in order to expose typing information to the store consumer via a series of Generics which reference the configuration object. This means that this Store wrapper enables IntelliSense (VSCode), Linters, and the TypeScript Compiler to be able to validate that code is actually using the store correctly.

How to install

Installation is fairly straightforward:

npm install --save vuex-tstore

How to use

Setting up the Vuex TStore is slightly more involved than you may be used to. In "vanilla" Vuex, type information will automatically propagate the state and context types into the function. In Vuex TStore, you'll need to define those types yourself:

import Vue from "vue";
import TStore from "vuex-tstore";
import { ActionContext } from "vuex";

// Not completely necessary, but will bind the vue.$tstore element to the TStore
Vue.use(TStore);

type State = {
  title: string
};
type Context = ActionContext<State, State>;

// If you assign this to Vuex StoreOptions<State>, it will break your bindings.
const options = {
  state: (): State => ({ title: "Hello, world!" }),
  getters: {
    title: (state: State) => state.title
  },
  mutations: {
    resetTitle: (state: State) => { state.title = ''; },
    setTitle: (state: State, payload: { title: string }) => { state.title = payload.title; }
  },
  actions: {
    resetTitle: async (context: Context) => context.commit('resetTitle'),
    setTitle: (context: Context, payload: { title: string }) => {
      setTimeout(() => context.commit('setTitle', payload), 1000);
    }
  }
};

const store = new TStore.Store(options);

However, once configured, the TStore will automatically deduct the parameter and return types on getters, mutations, actions, and state objects.

store.getters.title; // "Hello, world!"
store.mutations.resetTitle(); // ""
store.mutations.setTitle({ title: "foo" }); // "foo"
store.actions.resetTitle();
store.actions.setTitle({ title: "bar" });

Project Typedef

Since larger projects tend to be spread out more than what I was doing in the tests, you will probably need to overwrite the default project typedef values. This is the format I've used in other projects to get my IDE to recognize the store:

import StoreRoot from 'path/to/my/store';

declare module 'vue/types/vue' {
  interface Vue {
    $tstore: StoreRoot;
  }
}

Store Structure

| Object | Description | | ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | store.state | Provides a type-aware proxy to the Vuex State. | | store.getters | Provides a type-aware proxy to the Vuex Getters. | | store.mutations | Contains a type-aware proxy to the Vuex Mutations. The TStore equivalent to store.commit("mutate", payload) is store.mutations.mutate(payload). | | store.actions | Contains a type-aware proxy to the Vuex Actions. The TStore equivalent to store.dispatch("action", payload) is store.actions.action(payload). | | store.modules | Contains the registered modules on the Vuex Store. For example, a getter called store.getters["foo/bar"] would now live under store.modules.foo.getters.bar. |

Event Listeners

There are some new shorthand for registering event-handlers in this library.

Mutation Listeners

// call `unsub()` to remove the listener from the store.
const unsub = store.mutations.setTitle.listen(({ title }) => { alert(title); });

Action Listeners

const unsub_1 = store.actions.setTitle.before(({ title }) => console.log(`Updating title to ${title}`));
const unsub_2 = store.actions.setTitle.after(({ title }) => console.log(`Finished updating title to ${title}`));