]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
feat(types): SetupStoreDefinition
authorEduardo San Martin Morote <posva13@gmail.com>
Fri, 13 Oct 2023 09:22:53 +0000 (11:22 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Fri, 13 Oct 2023 09:22:59 +0000 (11:22 +0200)
packages/pinia/src/index.ts
packages/pinia/src/store.ts
packages/pinia/test-dts/typeHelpers.test-d.ts

index 9735ef37672c34b45b32986acfb899c72fbefdf7..fc28044f4d75fc0a673ed82f37772aff76f4b39e 100644 (file)
@@ -12,7 +12,12 @@ export type {
 } from './rootStore'
 
 export { defineStore, skipHydrate } from './store'
-export type { StoreActions, StoreGetters, StoreState } from './store'
+export type {
+  StoreActions,
+  StoreGetters,
+  StoreState,
+  SetupStoreDefinition,
+} from './store'
 
 export type {
   StateTree,
index e76381f0413583c03f612949130c729b892a6c5b..cf1d5be173e4ef3ad484236658cc3a25690cc58d 100644 (file)
@@ -964,3 +964,17 @@ export function defineStore(
 
   return useStore
 }
+
+/**
+ * Return type of `defineStore()` with a setup function.
+ * - `Id` is a string literal of the store's name
+ * - `SS` is the return type of the setup function
+ * @see {@link StoreDefinition}
+ */
+export interface SetupStoreDefinition<Id extends string, SS>
+  extends StoreDefinition<
+    Id,
+    _ExtractStateFromSetupStore<SS>,
+    _ExtractGettersFromSetupStore<SS>,
+    _ExtractActionsFromSetupStore<SS>
+  > {}
index 2bd7c9eb3788422e7def58514397a5a669f2509f..5e1a432d4204986f403220379e0a4644114729ad 100644 (file)
@@ -1,6 +1,7 @@
-import { StoreDefinition } from './'
-import { computed, ref } from 'vue'
+import { ComputedRef, Ref, computed, ref } from 'vue'
 import {
+  StoreDefinition,
+  SetupStoreDefinition,
   StoreState,
   StoreGetters,
   StoreActions,
@@ -60,10 +61,26 @@ expectType<{ n: number }>(storeState(useOptionsStore))
 
 expectType<{ double: number }>(storeGetters(useOptionsStore))
 
-expectType<{ n: number }>(
-  storeState(
-    defineStore('', {
-      state: () => ({ n: ref(0) }),
-    })
-  )
+expectType<
+  SetupStoreDefinition<
+    'a',
+    {
+      n: Ref<number>
+      double: ComputedRef<number>
+      increment: () => void
+    }
+  >
+>(
+  defineStore('a', () => {
+    const n = ref(0)
+    const double = computed(() => n.value * 2)
+    function increment() {
+      n.value++
+    }
+    return {
+      double,
+      increment,
+      n,
+    }
+  })
 )