Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[vuex] state field "foo" was overridden by a module with the same name at "foo" #1652

Closed
mspoulsen opened this issue Nov 28, 2019 · 5 comments

Comments

@mspoulsen
Copy link

Version

3.1.2

Reproduction link

https://gist.github.com/mspoulsen/f3a67e22a76ba124e606a0beae597974

Steps to reproduce

Create a minimal store using the gist.
Then try to access store.state.foo

Result: We get Typescript error: Property 'foo' does not exist on type 'RootState'.

What is expected?

A Typescript error is expected

What is actually happening?

We get the Typescript error and that is the problem


How would I be able to access store.state.foo without adding foo to RootState? And if I do I get this warning in 3.1.2:
[vuex] state field "foo" was overridden by a module with the same name at "foo"

@ktsn
Copy link
Member

ktsn commented Nov 28, 2019

Essentially duplicate of #1267

You can add your module state in RootState and cast the root state value to any as a work around.

@ktsn ktsn closed this as completed Nov 28, 2019
@mspoulsen
Copy link
Author

That wouldn't make the warning go away, right?

@ktsn
Copy link
Member

ktsn commented Nov 28, 2019

Yes it will go away.

interface RootState {
  foo: {} // declare your module state
}

const rootState: any = {
  
}

const storeConfig: StoreOptions<RootState> = {
  modules: {
    foo: {}
  },
  state: rootState
}

const store: Store<RootState> = new Vuex.Store(storeConfig)

@mspoulsen
Copy link
Author

That works, thanks! Can't wait for Vue3 and Typescript just working everywhere 🙂

@mcfarljw
Copy link

mcfarljw commented Jan 29, 2021

If you don't want to use any I also had luck overriding the object as the interface. Still not ideal but seemed to suppress the warning and typescript errors.

interface RootState {
  foo: {}
}

const rootState = {} as RootState

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants