]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
refactor(types): fix autocompletion
authorEduardo San Martin Morote <posva13@gmail.com>
Thu, 8 Apr 2021 09:19:39 +0000 (11:19 +0200)
committerEduardo San Martin Morote <posva@users.noreply.github.com>
Fri, 9 Apr 2021 11:08:56 +0000 (13:08 +0200)
src/mapHelpers.ts

index 4ed6f8b9aa65bb0f56fd4c076fcea215a844a21f..4eaaffba4e967153ff16f5c5236d194f877382fd 100644 (file)
@@ -192,59 +192,59 @@ type MapActionsObjectReturn<A, T extends Record<string, keyof A>> = {
 /**
  * Allows directly using actions from your store without using the composition
  * API (`setup()`) by generating an object to be spread in the `methods` field
- * of a component.
+ * of a component. The values of the object are the actions while the keys are
+ * the names of the resulting methods.
  *
  * @example
  * ```js
  * export default {
  *   methods: {
  *     // other methods properties
- *     ...mapActions(useCounterStore, ['increment', 'setCount'])
+ *     // useCounterStore has two actions named `increment` and `setCount`
+ *     ...mapActions(useCounterStore, { moar: 'increment', setIt: 'setCount' })
  *   },
  *
  *   created() {
- *     this.increment()
- *     this.setCount(2) // pass arguments as usual
+ *     this.moar()
+ *     this.setIt(2)
  *   }
  * }
  * ```
  *
  * @param useStore - store to map from
- * @param keys - array of action names to map
+ * @param keyMapper - object to define new names for the actions
  */
 export function mapActions<Id extends string, S extends StateTree, G, A>(
   useStore: StoreDefinition<Id, S, G, A>,
-  keys: Array<keyof A>
-): MapActionsReturn<A>
+  keyMapper: Record<string, keyof A>
+): MapActionsObjectReturn<A, Record<string, keyof A>>
 /**
  * Allows directly using actions from your store without using the composition
  * API (`setup()`) by generating an object to be spread in the `methods` field
- * of a component. The values of the object are the actions while the keys are
- * the names of the resulting methods.
+ * of a component.
  *
  * @example
  * ```js
  * export default {
  *   methods: {
  *     // other methods properties
- *     // useCounterStore has two actions named `increment` and `setCount`
- *     ...mapActions(useCounterStore, { moar: 'increment', setIt: 'setCount' })
+ *     ...mapActions(useCounterStore, ['increment', 'setCount'])
  *   },
  *
  *   created() {
- *     this.moar()
- *     this.setIt(2)
+ *     this.increment()
+ *     this.setCount(2) // pass arguments as usual
  *   }
  * }
  * ```
  *
  * @param useStore - store to map from
- * @param keyMapper - object to define new names for the actions
+ * @param keys - array of action names to map
  */
 export function mapActions<Id extends string, S extends StateTree, G, A>(
   useStore: StoreDefinition<Id, S, G, A>,
-  keyMapper: Record<string, keyof A>
-): MapActionsObjectReturn<A, Record<string, keyof A>>
+  keys: Array<keyof A>
+): MapActionsReturn<A>
 /**
  * Allows directly using actions from your store without using the composition
  * API (`setup()`) by generating an object to be spread in the `methods` field
@@ -253,16 +253,10 @@ export function mapActions<Id extends string, S extends StateTree, G, A>(
  * @param useStore - store to map from
  * @param keysOrMapper - array or object
  */
-export function mapActions<
-  Id extends string,
-  S extends StateTree,
-  G,
-  A,
-  KeyMapper extends Record<string, keyof A>
->(
+export function mapActions<Id extends string, S extends StateTree, G, A>(
   useStore: StoreDefinition<Id, S, G, A>,
-  keysOrMapper: Array<keyof A> | KeyMapper
-): MapActionsReturn<A> | MapActionsObjectReturn<A, KeyMapper> {
+  keysOrMapper: Array<keyof A> | Record<string, keyof A>
+): MapActionsReturn<A> | MapActionsObjectReturn<A, Record<string, keyof A>> {
   return Array.isArray(keysOrMapper)
     ? keysOrMapper.reduce((reduced, key) => {
         reduced[key] = function (this: Vue, ...args: any[]) {
@@ -270,10 +264,13 @@ export function mapActions<
         } as Store<string, StateTree, {}, A>[keyof A]
         return reduced
       }, {} as MapActionsReturn<A>)
-    : Object.keys(keysOrMapper).reduce((reduced, key: keyof KeyMapper) => {
-        reduced[key] = function (this: Vue, ...args: any[]) {
-          return getCachedStore(this, useStore)[keysOrMapper[key]](...args)
-        } as Store<string, StateTree, {}, A>[keyof KeyMapper[]]
-        return reduced
-      }, {} as MapActionsObjectReturn<A, KeyMapper>)
+    : Object.keys(keysOrMapper).reduce(
+        (reduced, key: keyof Record<string, keyof A>) => {
+          reduced[key] = function (this: Vue, ...args: any[]) {
+            return getCachedStore(this, useStore)[keysOrMapper[key]](...args)
+          } as Store<string, StateTree, {}, A>[keyof Record<string, keyof A>[]]
+          return reduced
+        },
+        {} as MapActionsObjectReturn<A, Record<string, keyof A>>
+      )
 }