[feature request] make vuex-class more type safe
See original GitHub issueI just read a blog post which explained that TypeScript 2.1 has a new feature which makes it possible to type-check the keys of objects. Here’s the blog post.
So basically I’m imagining that it should also be possible to do this with vuex, since the store object is just a regular object, right?
So my feature request means that if I have a store like this:
const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
increment (state) {
state.count++
}
}
})
And I have a component like this:
import Vue from 'vue'
import Component from 'vue-class-component'
import {
State,
Getter,
Action,
Mutation,
namespace
} from 'vuex-class'
const ModuleGetter = namespace('path/to/module', Getter)
@Component
export class MyComp extends Vue {
@State('count') count
@Mutation('incremet') increment
}
That the type-annotation would make typescript understand that the count
variable of MyComp
is a number, since the state variable is a number. And also that typescript would throw an error during compilation, because it can see that increment
was misspelled.
Do you think you could try to implement that? Even just having one of those two features would already be awesome.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:18
- Comments:26 (4 by maintainers)
Top GitHub Comments
Is there any progress?
What can I do to know the state type in the Vue component?
@ktsn I have a idea, like vue-class-component Use a @VuexComponent decorate MyVuexClass property->state get->getter set->mutation method->action and when combining the store, use ClassToModule method , transform a new instance(maybe can use singleton pattern) to a object like this:
In our Vue Component , we can declare a instance(myStore) from MyVuexClass , because we had inject the store in root , we also can access state by myStore
I am new with TypeScript , this is just a imagine, is it possible? 😄