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

v4.1.0

Published

Type helpers for vuex.

Downloads

149

Readme

vuex-typing

vuex-typing is a small helper library for typing in vuex. vuex does not provide typing such as dispatch or getter by default. vuex-typing allows you to type in vuex with minimal additional code.

Installation

vuex-typing supports vue@3 and vue@4.

$ yarn add vue@next vuex@next vuex-typing

How to type in vuex ?

Typing is done by passing the vuex module definition to defineModule provided by vuex-typing. By passing the module definition through the defineModule function, we can pick up the types from the implementation and avoid duplicating the type declaration and implementation. This is the same approach used in vue3's defineComponent.

import { defineModule } from "vuex-typing"

export const textModuleName = "text"

export const textModule = defineModule(
  {
    state: (): { text: string | undefined } => ({
      text: undefined,
    }),
    getters: {
      txt: (state) => state.text,
    },
    mutations: {
      setText: (state, text: string) => {
        state.text = text
      },
    },
  },
  {
    SET_TEXT: ({ commit }, text: string) => {
      commit("setText", text)
    },
  }
)

The first argument is the standard options of the vuex module, and only the actions are passed as the second argument to ensure that the context is properly typed.

The return value is a configuration object that merges the two options passed to the function with namespaced: true, so you can register it as a module in the store.

// store/index.ts
import { createStore } from "vuex"
import { TypedStore } from "vuex-typing"

import { textModuleName, textModule } from "./modules/text"

export type RootState = {}
export type ModuleType = {
  [textModuleName]: typeof textModule
}

export type RootStore = TypedStore<RootState, ModuleType>

export const store = createStore<RootState>({
  modules: {
    [textModuleName]: textModule,
  },
})

Now that RootStore is typed for the module, we can type this.$store and useStore as recommended by the vuex formula.

// store/util.ts
import { InjectionKey } from "vue"
import { useStore as baseUseStore } from "vuex"
import type { RootStore } from "./index"

export const key: InjectionKey<RootStore> = Symbol()

export function useStore(): RootStore {
  return baseUseStore(key)
}
// @types/vuex.d.ts
import type { RootStore } from "../src/store/index"

declare module "@vue/runtime-core" {
  interface ComponentCustomProperties {
    $store: RootStore
  }
}

The configuration is now complete. Typing will be provided when using useStore or this.$store as usual. The whole example can be found under the example directory

Using other actions in the module

When using defineModule, the context will be typed, but only the dispatch will need to inject a type definition by using LocalDispatch.

import { defineModule, LocalDispatch } from "vuex-typing"

export const counterModuleName = "counter"

export const counterModule = defineModule(
  // ...
  {
    INCREMENT: ({ commit }): void => {
      commit("increment")
    },
    PLUS_N_LOOP: ({ dispatch: _dispatch }, n: number) => {
      const dispatch: LocalDispatch<CounterModule["actions"]> = _dispatch

      for (const _i of new Array(n)) {
        dispatch("INCREMENT")
      }
    },
  }
)

type CounterModule = typeof counterModule

typing with mapX helper function

vuex-typing provides types that override the typedefs of helper functions such as mapGetters.

// store/util.ts
import { InjectionKey } from "vue"
import {
  useStore as baseUseStore,
  mapGetters as baseMapGetters,
  mapState as baseMapState,
  mapActions as baseMapActions,
} from "vuex"
import { MapState, MapGetters, MapActions } from "vuex-typing"
import type { RootStore, ModuleType } from "."

export const key: InjectionKey<RootStore> = Symbol()

export function useStore(): RootStore {
  return baseUseStore(key)
}

export const mapState = baseMapState as unknown as MapState<
  RootStore["state"],
  ModuleType
>

export const mapGetters = baseMapGetters as unknown as MapGetters<ModuleType>
export const mapActions = baseMapActions as unknown as MapActions<ModuleType>

Use helper functions with overriding type definitions instead of the built-in helper functions

- import { mapGetters } from 'vuex'           // untyped
+ import { mapGetters } from '../store/util'  // typed

export default defineComponent({
  computed: {
    ...mapGetters({ /* options */ })
  }
})

Not supported

Non-module typing is minimal. The root store supports typing only for state.

Modules are only supported with namespaced: true, and defineModules will automatically register this option. Nested modules are also not supported.