]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
fix(types): correct order for hydrate function
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 28 Jul 2021 12:45:25 +0000 (14:45 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 28 Jul 2021 12:45:25 +0000 (14:45 +0200)
docs/core-concepts/plugins.md
src/store.ts
src/types.ts
test-dts/customizations.test-d.ts

index 703976f2c70721b37e288118da5ca522d0bf9bab..00e5656374bb6703869bc0b454bdf8fa8b4c094f 100644 (file)
@@ -221,6 +221,24 @@ pinia.use(({ options, store }) => {
 })
 ```
 
+Note that custom options are passed as the 3rd argument when using the setup syntax:
+
+```js
+defineStore(
+  'search',
+  () => {
+    // ...
+  },
+  {
+    // this will be read by a plugin later on
+    debounce: {
+      // debounce the action searchContacts by 300ms
+      searchContacts: 300,
+    },
+  }
+)
+```
+
 ## TypeScript
 
 Everything shown above can be done with typing support, so you don't ever need to use `any` or `@ts-ignore`.
index 6c0d1d1b26267d8c8bca8e61213ae5c5275c33dd..e69e779bed58fc78f0c7860c23b7e03672b2bf39 100644 (file)
@@ -561,7 +561,6 @@ function createSetupStore<
   })
 
   if (initialState) {
-    // @ts-expect-error: initialState doesn't match
     ;(options.hydrate || innerPatch)(store, initialState)
   }
 
index c552a372c96ebe94a47108f5cdd5b815f08e2ef8..7a38a2e5bacedba4e1388e504c9681ce0910da36 100644 (file)
@@ -550,7 +550,7 @@ export interface DefineStoreOptions<
   S extends StateTree,
   G /* extends GettersTree<S> */,
   A /* extends Record<string, StoreAction> */
-> extends DefineStoreOptionsBase<Store<Id, S, G, A>, S> {
+> extends DefineStoreOptionsBase<S, Store<Id, S, G, A>> {
   /**
    * Unique string key to identify the store across the application.
    */
@@ -591,7 +591,7 @@ export interface DefineSetupStoreOptions<
   S extends StateTree,
   G,
   A /* extends ActionsTree */
-> extends DefineStoreOptionsBase<Store<Id, S, G, A>, S> {
+> extends DefineStoreOptionsBase<S, Store<Id, S, G, A>> {
   /**
    * Extracted actions. Added by useStore(). SHOULD NOT be added by the user when
    * creating the store. Can be used in plugins to get the list of actions in a
index 71746081cec920ca078da24895a2567630fa461b..a60c14f6ab902278ffa8e0c9c55e562ad89f9ca2 100644 (file)
@@ -25,12 +25,13 @@ declare module '../dist/pinia' {
     stateOnly: number
   }
 
-  export interface DefineStoreOptions<Id, S, G, A> {
-    debounce?: {
-      // Record<keyof A, number>
-      [k in keyof A]?: number
-    }
+  export interface DefineStoreOptionsBase<S, Store> {
+    debounce?: Partial<Record<keyof StoreActions<Store>, number>>
   }
+
+  // export interface DefineStoreOptions<Id, S, G, A> {
+  //   debounce?: Partial<Record<keyof A, number>>
+  // }
 }
 
 const pinia = createPinia()
@@ -89,6 +90,23 @@ const useStore = defineStore({
   },
 })
 
+defineStore(
+  'withSetup',
+  () => {
+    function one() {}
+    function two() {}
+    function three() {}
+
+    return { one, two, three }
+  },
+  {
+    debounce: {
+      one: 200,
+      two: 300,
+    },
+  }
+)
+
 type Procedure = (...args: any[]) => any
 
 function debounce<F extends Procedure>(fn: F, time: number = 200) {
@@ -100,11 +118,12 @@ expectType<{
 }>(mapStores(useStore))
 
 pinia.use(({ options, store }) => {
-  if (options.debounce) {
-    return Object.keys(options.debounce).reduce((debouncedActions, action) => {
+  const { debounce: debounceOptions } = options
+  if (debounceOptions) {
+    return Object.keys(debounceOptions).reduce((debouncedActions, action) => {
       debouncedActions[action] = debounce(
         store[action],
-        options.debounce![action as keyof typeof options['actions']]
+        debounceOptions[action]
       )
       return debouncedActions
     }, {} as Record<string, (...args: any[]) => any>)