From 5ab811144f5df854858601c94887072b7e38e88e Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Thu, 8 Apr 2021 11:19:39 +0200 Subject: [PATCH] refactor(types): fix autocompletion --- src/mapHelpers.ts | 59 ++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 31 deletions(-) diff --git a/src/mapHelpers.ts b/src/mapHelpers.ts index 4ed6f8b9..4eaaffba 100644 --- a/src/mapHelpers.ts +++ b/src/mapHelpers.ts @@ -192,59 +192,59 @@ type MapActionsObjectReturn> = { /** * 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( useStore: StoreDefinition, - keys: Array -): MapActionsReturn + keyMapper: Record +): MapActionsObjectReturn> /** * 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( useStore: StoreDefinition, - keyMapper: Record -): MapActionsObjectReturn> + keys: Array +): MapActionsReturn /** * 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( * @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 ->( +export function mapActions( useStore: StoreDefinition, - keysOrMapper: Array | KeyMapper -): MapActionsReturn | MapActionsObjectReturn { + keysOrMapper: Array | Record +): MapActionsReturn | MapActionsObjectReturn> { return Array.isArray(keysOrMapper) ? keysOrMapper.reduce((reduced, key) => { reduced[key] = function (this: Vue, ...args: any[]) { @@ -270,10 +264,13 @@ export function mapActions< } as Store[keyof A] return reduced }, {} as MapActionsReturn) - : Object.keys(keysOrMapper).reduce((reduced, key: keyof KeyMapper) => { - reduced[key] = function (this: Vue, ...args: any[]) { - return getCachedStore(this, useStore)[keysOrMapper[key]](...args) - } as Store[keyof KeyMapper[]] - return reduced - }, {} as MapActionsObjectReturn) + : Object.keys(keysOrMapper).reduce( + (reduced, key: keyof Record) => { + reduced[key] = function (this: Vue, ...args: any[]) { + return getCachedStore(this, useStore)[keysOrMapper[key]](...args) + } as Store[keyof Record[]] + return reduced + }, + {} as MapActionsObjectReturn> + ) } -- 2.47.3