Implementing Vuex 4 modules within Vue 3 using TypeScript and troubleshooting the cyclical dependency linting issue

Vue 3.0 has recently launched its stable v3.0.0 'One Piece' release, while Vuex 4 is currently in v4.0.0-beta.4.

Although official examples on utilizing Vuex 4 modules in TypeScript are lacking...

For better code management as modules expand, I aim to store state, mutations, getters, and actions in separate files.

To address this need, I've assembled a functional example repository on Github and Codesandbox.

Extracting guidance from these resources:

However, a few challenges remain unresolved:

1. Resolving linting errors due to dependency cycles

The typings in module's index.ts, actions.ts, and getters.ts rely on importing RootState from the main store, resulting in linting errors when using ESLint Airbnb config.

Despite not impacting store functionality directly, I seek solutions without resorting to disabling or suppressing rules.

Further exploration may determine if Standard and Prettier configs trigger similar issues, indicating potential linkage with Airbnb rules.

2. Implementing modules without actions successfully

My attempts to configure the profile module sans actions encountered type specification challenges, including an existing NON_ACTION action type within the example code.

3. Effectively applying the namespaced: true module option and its impact on component syntax

Configured with namespaced: true, the profile store necessitates a distinct usage syntax in components, unlike the documents module which lacks this option and experiences dispatch issues with action types.

PS. Any suggestions for further code optimization are appreciated through comments, answers, and pull requests!

Update 27/09/20: Noticed VS Code intellisense fail to display store instance type information post useStore function passage within components, functioning only when imported in .ts files.

Answer №1

I recently released a package aimed at simplifying the use of Vuex 4 with Typescript. While it may not be flawless, I believe it addresses 95% of the challenges we encountered. I welcome any feedback or suggestions for improvement.

It is important to note that this package requires TypeScript version 4.1 or higher in order to benefit from template literal support.

https://www.npmjs.com/package/typed-vuex-wrapper

For what it's worth, the initial concept was heavily influenced by your work on modules, but has been expanded to include support for mapState/mapActions and other features, all with fully typed access. Use of TS 4.1 enables safe namespaced access without relying on magic strings.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Data-api configuration for bootgrid ajax

According to the BootGrid documentation, in order to set the HTTP method to GET or enable AJAX, one must use the method and ajax attributes in JavaScript. However, the Data-API example demonstrates the use of data-url and data-ajax, leading me to conclude ...

Create PDF files on-the-fly using the html-pdf module

Recently, I've been using the npm package html-pdf for generating PDF invoices dynamically. However, I encountered a problem where instead of saving values to the PDF file, it was saving ejs code. Does anyone have any insight on why this might be happ ...

Re-Rendering Component in React Continuously Keeps Checkbox Checked Event Flowing

I am working on a material ui checkbox formgroup that is generated dynamically based on data received from an API. Essentially, the user is presented with a question and a set of answers. The user checks all the valid answers and clicks 'next'. I ...

Expanding the padding of the selected item to create more breathing room

Upon examining the following: https://jsfiddle.net/h8kmove5/27/ Let's say you select 9 at the bottom. The display shows 8 9 10. My intention is to create some additional space on either side of 9, or any other active item for that matter. This way, ...

Clicking on Only One Card in Material UI React

I've encountered an issue with my code: const useStyles = makeStyles({ card: { maxWidth: 345, }, media: { height: 140, }, }); export default function AlbumCard(props) { const classes = useStyles(); let ...

Building Your Own Array Object in JavaScript

Yes, it may seem crazy at first glance, but let me clarify things a bit. When using Jquery, for instance $('div'), it returns an Array Collection similar to this: [div#container, div#header, div#logo]. The interesting part is that primitive Arra ...

Declaring an array that holds Angular components in Angular 11: What's the best approach?

I'm trying to implement a feature in my Angular component where I can create a list that displays content from other components. My current approach involves declaring an array that contains references to different Angular components: import { Compone ...

Leveraging react-query with next-mdx-remote MDX Components?

I have been using next-mdx-remote in my next.js project. One of the components I've passed to it makes API calls using axios, which has been working well. However, I now want to switch to using react-query. When implementing this change, I encountered ...

Why is my model not being updated when selecting an option in Angular UI Select-UI?

I want to streamline the use of select-ui by creating a wrapper in a directive. This way, I can easily update the control in one place and avoid the hassle of scattering it all over my site. Despite building a wrapper, I'm facing an issue where the v ...

Issues arise when attempting to transfer strings through ajax to a Node.js server that is utilizing express.js

On my website, I am encountering a problem where certain characters are being lost when I send strings to my Node.js server. // Client: microAjax("/foo?test="+encodeURI("this is ++ a test"), function callback(){}); // Server: app.get('/foo',fun ...

Strategies for evaluating a Promise-returning imported function in Jest

I am currently facing an issue with a simple function that I need to write a test for in order to meet the coverage threshold. import { lambdaPromise } from '@helpers'; export const main = async event => lambdaPromise(event, findUsers); The ...

Backbone - NestedModels - Issues with nested sets not triggering 'change' event

I have incorporated the Backbone nested plugin into my project. The way I set up my binding is as follows : var view = Backbone.View.extend({ initialize: function(params) { this.model.bind('change', _.bind(this.rerender, this)); ...

Converting an array into an object in Angular for query parameters

In my Angular 12 application, I have an array of objects that I need to convert into query parameters in order to route to a generated URL. The desired query parameters should look like this: Brand:ABC:Brand:XYZ:Size:13x18:Size:51x49x85 [{ "values&q ...

Having trouble loading CSS in an express view with a router

I am encountering an issue where I am unable to load my CSS into a view that is being rendered from a Router. The CSS loads perfectly fine for a view rendered from app.js at the root of the project directory. Below is my current directory structure, node_ ...

Error with Expression Engine: PHP function cannot be executed

I have been attempting to execute a PHP Script with ExpressionEngine tags using Ajax on my webpage. I followed the documentation and set up the PHP script accordingly, but it appears that I am unable to call the function in the PHP script. There must be so ...

Angular 5's data display glitch

Whenever I scroll down a page with a large amount of data, there is a delay in rendering the data into HTML which results in a white screen for a few seconds. Is there a solution to fix this issue? Link to issue I am experiencing HTML binding code snippe ...

Electron.js issue: ipcRenderer and ipcMain leading to a white screen problem

I am currently working on a desktop application using Electron, Vue, and Vuetify. However, I have encountered an issue where sending data from the rendererProcess to mainProcess using IPC results in a white blank screen. I'm unsure of what is causing ...

Tips for dynamically displaying images in both horizontal and vertical orientations

I would like to showcase images in the imageList. Here is what I want: AB CD How can this be achieved? It's not a matter of being even or odd Perhaps the list could look something like this: ABCDE FG I simply want a new row or display:block when ...

The plugin data containing 'data:application/pdf;base64,JVBERi0xLjQKJ**' was declined for loading. Content security policy cannot be configured

My approach to enforcing content security policy involves using the following code snippet: <meta charset="UTF-8" http-equiv="content-security-policy" content="object-src 'self' data:" /> However, when this i ...

Performing a jQuery ajax POST request to log users in

I am currently facing a challenge with my .ajax request being made through a form for the purpose of logging in. Upon submitting my form, I am not receiving any response from either the success or error functions. Interestingly, even when I inserted an a ...