From: Eduardo San Martin Morote Date: Thu, 8 Apr 2021 09:19:39 +0000 (+0200) Subject: refactor(types): fix autocompletion X-Git-Tag: v2.0.0-alpha.11~17 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5ab811144f5df854858601c94887072b7e38e88e;p=thirdparty%2Fvuejs%2Fpinia.git refactor(types): fix autocompletion --- 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> + ) }