Skip to content

How do I type a stor if _GettersTree is marked as for internal use only? #751

Discussion options

You must be logged in to vote

TS can infer the types without you having to specify them all. This should be all you need:

interface State {
  list: Apartment[];
}

export const useApartmentsStore = defineStore(
  "apartments-pinia",
  {
    state: (): State => ({
      list: [],
    }),
    getters: {
      getById: (state) => (id: string) => state.list.find((apartment) => apartment.id === id),
    },
  }
);

I also converted the state function to an arrow function as it improves type safety for some, see https://pinia.esm.dev/core-concepts/state.html#state

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@Azema4ka
Comment options

@BenShelton
Comment options

@Azema4ka
Comment options

Answer selected by Azema4ka
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants