Typescript error when trying to call action from getter #450
Answered
by
posva
simshaun
asked this question in
Help and Questions
-
Is this a bug? // Contrived example
import { defineStore } from 'pinia'
export const useTestStore = defineStore({
id: 'test',
state() {
return {}
},
getters: {
cssProperties(): string[] {
const props = []
for (const prop of ['foo', 'bar']) {
props.push(this.getProp(prop))
}
return props
},
},
actions: {
getProp(prop: string): string {
return prop
},
},
}) |
Beta Was this translation helpful? Give feedback.
Answered by
posva
Apr 26, 2021
Replies: 1 comment 2 replies
-
This is rather intentionally left out because getters being computed properties they should not have side effects and therefore not call actions. It will work because If you want to extract code into a function, it should rather be a regular function that takes as an argument whatever is needed to compute a value |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
simshaun
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is rather intentionally left out because getters being computed properties they should not have side effects and therefore not call actions. It will work because
this
is the store but it isn't typed with the existing actions.If you want to extract code into a function, it should rather be a regular function that takes as an argument whatever is needed to compute a value