Skip to content

Type of this of actions in setup (composition API) stores #1509

Discussion options

You must be logged in to vote

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:

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>

Replies: 5 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by posva
Comment options

You must be logged in to vote
2 replies
@posva
Comment options

posva Aug 3, 2022
Maintainer

@segevfiner
Comment options

Comment options

You must be logged in to vote
1 reply
@TonnyWildeman
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants