pinia icon indicating copy to clipboard operation
pinia copied to clipboard

Warn if two stores are defined with the same name

Open Soviut opened this issue 2 years ago • 2 comments

What problem is this solving

I have a store. I copied it to make a second store but forgot to change the name of the second store.

This resulted in very subtle bugs where store state that should never be able to be undefined was appearing as undefined.

It should be noted that these stores are being intentionally used in an un-strict manner where the state is being mutated directly.

store/invoices.ts

export const useStore = defineStore('invoices', () => {

store/templates.ts

export const useStore = defineStore('invoices', () => {

Proposed solution

Warn if multiple stores share the same name.

Describe alternatives you've considered

Using the Vue Dev Tools, I was able to see that only one store existed and it sometimes had the values of the first store and sometimes values from the second store.

Soviut avatar Jun 26 '22 07:06 Soviut

I had a simlar issue, where I had created a state variable and a function with the same name.

I was getting some 'computed' error on my nuxt project until I did some digging

jamiecarter7 avatar Aug 20 '22 11:08 jamiecarter7

Since Vuex there is one thing in my mind that could resolve the issue with possible name duplicates. Personally I have one big enum for a whole project (with store names) but it feels like a workaround.

Maybe instead of using string as a store name, there could be at least an option to use symbol. It's description could serve as displayed store name in dev tools. That would give unique id for every store, even if the name is the same. It could still warn about duplicated names (symbol descriptions) but without failing functionally.

themeler avatar Sep 04 '23 08:09 themeler