Type of this of actions in setup (composition API) stores #1509
-
When defining actions in setup stores (composition API) what should the type of import { ref } from 'vue';
import { defineStore } from 'pinia';
export const useExampleStore = defineStore('example', () => {
const x = ref(0);
const y = ref('foo');
function wat(this: ???) {
this.$patch({
x: 123,
y: 'bar',
});
}
return {
x,
y,
wat,
};
}); |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 3 replies
-
there is no As for the type of export const useExampleStore = defineStore('example', () => {
const x = ref(0);
const y = ref('foo');
// Probably needed to type the return value
function wat(this: Store): void {
this.$patch({
x: 123,
y: 'bar',
});
}
return {
x,
y,
wat,
};
});
type Store = ReturnType<typeof useExampleStore> |
Beta Was this translation helpful? Give feedback.
-
Not inside the setup store itself. But in an action, which is a function with its own separate |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
@posva What do you mean? Should we just skip using |
Beta Was this translation helpful? Give feedback.
-
#1509 (comment) @posva or someone else? Can you please reply? |
Beta Was this translation helpful? Give feedback.
there is no
this
and no$patch()
within setup stores. For setup stores,$patch()
is only available when consuming the stores.As for the type of
this
, I think you could do: