The specified key is not found in the 'StoreOptions<State>' type of Vuex 4, in combination with Vue 3 and Typescript

I'm navigating the process of setting up a Vuex 4 Store with Typescript and Vue3, despite having limited experience with typescript.

Following the Vuex Tutorial for the initial setup, I almost entirely copy-pasted the code. The only difference is that in my State interface, I included a key of type Station.

An error has surfaced:

TS2345: Argument of type '{ station: {}; isOverlayActive: boolean; }' is not assignable to parameter of type 'StoreOptions<State>'.
  Object literal may only specify known properties, and 'station' does not exist in type 'StoreOptions<State>'.

This is what my Station interface looks like:

export default interface Station {
  uuid: string;
  name: string;
  teaser: {
    src: string;
    title: string;
  };
  event: {
    uuid: string;
    name: string;
    logo: {
      src: string;
      title: string;
    };
  };
  logo: {
    src: string;
    title: string;
  };
  theme: {
    mainColor: string;
    textColor: string;
  };
}

and this snippet is from my index.ts file where I define the store and encounter the error:

import { InjectionKey } from "vue";
import { createStore, useStore as baseUseStore, Store } from "vuex";
import Station from "@/interfaces/station";

export interface State {
  station: Station;
  isOverlayActive: boolean;
}

export const key: InjectionKey<Store<State>> = Symbol();

export const store = createStore<State>({
  station: {}, // this is where the compiler indicates the error
  isOverlayActive: false,
});

export function useStore(): Store<State> {
  return baseUseStore(key);
}

Despite my attempts to troubleshoot by changing the station property to different types within the Store interface and store object, the same error persists.

Could someone point out where I might be going wrong? My ultimate aim is to have a well-typed store.

Answer №1

When working with the createStore function, make sure to enclose it within the state property for proper functionality.

For more information, refer to: https://vuex.vuejs.org/guide/typescript-support.html#simplifying-usestore-usage

Instead of:

export const store = createStore<State>({
  station: {}, // this will trigger a compiler error
  isOverlayActive: false,
});

The correct way is:

export const store = createStore<State>({
  state: {
    station: {},
    isOverlayActive: false,
  }
});

Keep in mind that an error may still occur if properties like uuid, name, etc. are not defined properly within {}.

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

Custom template for label in Vue 2 select2 framework

Is it possible to customize the label slot in a similar way to how we can change the template for the option slot in Vue Select? <v-select inputId="originsId" :options="origins" label="city" placeholder="Search..."> <template slot="option" ...

Establishing a connection between TypeScript and MongoDB

Whenever I try to add the mongo connection to the request, I encounter an error. The error message says: 'Property 'dbClient' does not exist on type 'Request<ParamsDictionary>'. I want the connection to be accessible witho ...

Modifying the attributes/properties within a class of a bootstrap button

Here is the code that I have: <b-button id="search-button" size="md" class="mt-1 w-100" type="submit" @click="someEvent()" >Example </b-button If we imagine calling someEvent() and wanting to modi ...

What is the alternative parameter to use instead of onChange in React Router v4?

Having an issue with the onChange Prop in TypeScript and React JS: I am encountering an error message saying "No overload matched this call." <HashRouter> <Switch> <Route path="/" ...

What can be done to avoid causing other elements to blur when clicking a button?

Imagine a scenario where there is a textarea and a button present. If the button is clicked, typically the textarea will blur. However, in this case, I do not want that to happen. I am seeking advice on how to prevent the textarea from blurring u ...

Have Babel transpile configuration files into CommonJS format

I'm having trouble getting my Nuxt app to work with Jest for testing. I have a setupFilesAfterEnv property set with a setupTests.ts file that imports a translation file. The translations file uses ES6 import/export module syntax, but it seems like my ...

Troubleshooting Nuxtjs configuration issue with setting up Github Pages

Trying to set up Github Pages deployment for NuxtJS but facing issues with the configuration file. I followed the documentation, but still can't get it right. The documentation suggests adding this code snippet in the file, which I tried multiple tim ...

Avoid saying the same thing more than once

Within my Typescript class, I have the following structure: class C { #fsm (...) startFoo(name: string) { this.#fsm.send('FOO', name) return this } startBar(name: string) { this.#fsm.send('BAR', name) return th ...

Transform JSON reply in JavaScript/Typescript/Angular

Looking for assistance with restructuring JSON data received from a server API for easier processing. You can find the input JSON file at assets/input-json.json within the stackblitz project: https://stackblitz.com/edit/angular-ivy-87qser?file=src/assets/ ...

How to ensure Service is loaded before App Component in Angular 6?

My Data service is responsible for fetching the JSON Object value, however all components load before the data service finishes loading. This results in undefined values when I call the service method from components. ...

Error in TypeScript detected for an undefined value that was previously verified

I have developed a function that can add an item to an array or update an item at a specific index if provided. Utilizing TypeScript, I have encountered a peculiar behavior that is puzzling me. Here is the Playground Link. This simple TypeScript functio ...

Supply additional parameters to the method decorator within an Angular component

Imagine a scenario where there are multiple methods requiring the addition of a confirmation dialog. In order to streamline this process, a custom decorator is created. @Component({...}) export class HeroComponent { constructor(private dialog: MatDialog ...

Is there a way to reload the entire page when clicking on a link in NuxtJS?

I am working on a Nuxt project and I am looking for a way to reload the pages every time a user visits any page for SEO purposes. Is there a way to achieve this using Routers? Currently, my app is set up with the default behavior of NUXT which "renders on ...

By incorporating current information, why is there a necessity to generate a fresh entry?

I'm attempting to retrieve data from an existing table, but when I update it, a new row is created with the same data. Below is the provided source code. (ProductInfo --JPA) @ManyToOne(cascade = CascadeType.ALL) @JoinColumn(name = "user_id" ...

Heroku error: unable to locate tsc despite exhaustive troubleshooting efforts

I've been attempting to deploy a basic nodejs app on heroku, but I keep encountering the error mentioned above. Despite trying various solutions provided here, nothing seems to resolve the issue. Here's a summary of what I've attempted so fa ...

Obtain characteristics of the primary element in Ionic version 2 or 3

After creating and opening my Sqlite database and saving the SQLiteObject in a variable within my app.component.ts, I now need to retrieve these attributes in my custom ORM. The ORM extends to other providers to describe the title and field of my tables. ...

Guide to automatically blur the input field and toggle it upon clicking the checkbox using Angular

<input (click)="check()" class="checkbox" type="checkbox"> <input [(ngModel)]="second_month" value="2019-09" type="month" [class.blurred]="isBlurred">> I need the second input(type=month) field to be initially blurred, and only unblur ...

Does combineLatest detach this from an angular service function?

Check out this test service on Stackblitz! It utilizes the combineLatest method inside the constructor to invoke a service method: constructor() { console.log("TEST SERVICE CONSTRUCTED") this.setParameters.bind(this) this.assignFixedParamete ...

Reducing FLOUT in Vue Components: A Guide to Minimizing Markup Rendering

My challenge involves displaying a list of server-rendered items, each with a countdown timer. Initially, when the view component is nested within each dom element, there appears to be nothing on the screen. However, suddenly, POP, the view component exhib ...

What is the best way to implement React ErrorBoundary in conjunction with redux-observable?

When dealing with asynchronous code, React Error Boundaries may not function as expected. In my case, I am using redux-observable and rxjs to retrieve data from an API. To handle errors, I am trying to utilize the catchError function provided by rxjs. I ...