Skip to content

How to get typescript to behave when using piniaOptions #737

Answered by CodeDredd
garma83 asked this question in Q&A
Discussion options

You must be logged in to vote

Hi,

that's a hard one. I think you are more advanced in typescript than me ^^.
I tried now for some time making a solution.
I don't know if there is a way to do it better, but for now i found this solution which i can implement. What do you think about it?

composables/useDataStore.ts

import type { DefineStoreOptionsBase } from 'pinia'
import { defineStore } from 'pinia'
import { useStoreActions } from './useStoreActions'

export function useDataStore<S extends DataStoreState, T extends DataStore = DataStore>(
  id: string,
  options: DefineStoreOptionsBase<S, T>,
) {
  return defineStore(id, {
    state: () => ({ data: {} } as S),
    actions: useStoreActions(),
    ...options,
  })
}

ex…

Replies: 1 comment 2 replies

Comment options

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

@CodeDredd
Comment options

Answer selected by garma83
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
enhancement New feature or request question Further information is requested
2 participants