
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
direct-vuex
Advanced tools
Just Vuex with typing. Compatible with the Vue 3 composition API.
First, add direct-vuex
to a Vue application:
npm install direct-vuex
The store can be implemented almost in the same way as usual. However, it is necessary to append as const
at the end of store and module implementation objects. It will help direct-vuex to better infer types.
Create the store:
import Vue from "vue"
import Vuex from "vuex"
import { createDirectStore } from "direct-vuex"
Vue.use(Vuex)
const { store, rootActionContext, moduleActionContext } = createDirectStore({
// … store implementation here …
} as const)
export default store
export { rootActionContext, moduleActionContext }
export type AppStore = typeof store
declare module "vuex" {
interface Store<S> {
direct: AppStore
}
}
The classic Vuex store is still accessible through the store.original
property. We need it to initialize the Vue application:
import Vue from "vue"
import store from "./store"
new Vue({
store: store.original, // Inject the classic Vuex store
// …
}).$mount("#app")
From a component, the direct store is accessible through the direct
property of the classic store:
const store = this.$store.direct
Or, you can just import it:
import store from "./store"
Then, the old way to call an action:
store.dispatch("myModule/myAction", myPayload)
… is replaced by the following wrapper:
store.dispatch.myModule.myAction(myPayload)
… which is fully typed.
Typed getters and mutations are accessible the same way:
store.getters.myModule.myGetter
store.commit.myModule.myMutation(myPayload)
Notice: The underlying Vuex store can be used simultaneously if you wish, through the injected this.$store
or store.original
.
Here is an example on how to do in a module:
import { moduleActionContext } from "./store"
const module = {
actions: {
async myAction(context, payload) {
const { commit, state } = myModuleActionContext(context)
// … Here, 'commit' and 'state' are typed.
}
}
}
export default module
export const myModuleActionContext = context => moduleActionContext(context, module)
And the same example, but in the root store:
actions: {
async myAction(context, payload) {
const { commit, state } = rootActionContext(context)
// … Here, 'commit' and 'state' are typed.
}
}
Warning: Types in the context of actions implies that TypeScript should never be allowed to infer the return type of an action from the context of the action. Indeed, this kind of typing would be recursive, since the context includes the return value of the action. When this happens, TypeScript passes the whole context to any
. Tl;dr; Declare the return type of actions where it exists!
With VS Code, our recommanded plugin is:
ms-vscode.vscode-typescript-tslint-plugin
)FAQs
Use and implement your Vuex store with TypeScript types. Compatible with the Vue 3 composition API.
The npm package direct-vuex receives a total of 2,389 weekly downloads. As such, direct-vuex popularity was classified as popular.
We found that direct-vuex demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.