-
Notifications
You must be signed in to change notification settings - Fork 11
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
how to write mapState function in vuex?? #76
Comments
One solution is to extend a class created dynamically. class Comp extends MapVuex({ getters: ['total'], state: ['count']}) {
}
declare MapVuex(...): { new(): { /* some dynamic properties */ }} You need some TypeScript-fu to implement MapVuex's type signature. |
this seems to work: @Component({
name : 'hello',
computed : {
...mapState({
count: 'count',
}),
},
})
export default class HelloComponent extends Vue {
count!: number
get countDisplay(): string {
return this.count.toString() + '!!!'
}
} |
I'm using default object component declaration, and found solution to mapping state inside components with Typescript advanced type. Should also work with above notation: First - you create state interface to use in vuex state declaration, like: export interface IStateRoot {
somethings?: string[]
} Next (one time operation) - create generic state mapper (I think best is to do it in type MapStateToComputed<S> = {
[K in keyof S]: () => S[K]
} And finally - use mapper to reuse vuex state declaration with map state in component: computed: {
...(mapState(['somethings']) as MapStateToComputed<IStateRoot>),
} When you'll use |
Hey
im using typescript syntax in my vuejs project that integrated with vuex
i wana use mapState method as computed in my .ts file but i got syntax error
currently i am using your suggested syntax for computed function,i mean
if you read the vuex docs you will know using this way is so repetitive and so boring instead using mapState is very easy and useful in large application
so i wana use mapState in Typescript component and i dont know the true way.
i tried below way to use mapState function it was wrong and didnt work :) :)
I'll be grateful if you help me.
The text was updated successfully, but these errors were encountered: