Obtaining type of the store's instance through typeof
#1054
-
What problem is this solvingAllowing to declare a variable's type in TS enviroment (in advance) equal to the exact representation of the Pinia store, before the variable's value is assigned (of which type otherwise would be naturally inferred by the Proposed solutionWould it be possible to leverage
Describe alternatives you've consideredI tried using Is there any already-exisiting way to achieve what I highlighted in the above snippet? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Is this what you need? 🤔 const useCounterStore = defineStore('counter', () => {
const current = ref(0)
const double = computed(() => current.value * 2)
const increment = () => current.value++
return {
current,
double,
increment,
}
})
type UseNullStore = ReturnType<typeof defineStore>
type NullStore = ReturnType<UseNullStore>
type CounterStore = ReturnType<typeof useCounterStore>
type CounterStoreSGA = Omit<CounterStore, keyof NullStore>
|
Beta Was this translation helpful? Give feedback.
-
There is a situation when we need to define a // in stores/sample.ts
export const useSampleStore = defineStore("sample", <T>() => {
const data = ref<T>({})
})
// in sample.vue
<script setup lang="ts">
type SampleSchema = {
a: string;
b: string;
}
const sampleStore = useSampleStore<SampleSchema>() // error: Expected 0 type arguments, but got 1`
</script> But it gets a Typescript error: |
Beta Was this translation helpful? Give feedback.
Is this what you need? 🤔