From 0dee5b9a20ac1ea05827c510310cd94c930b73e6 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Sun, 2 May 2021 21:25:50 +0200 Subject: [PATCH] refactor: use correct type import --- src/mapHelpers.ts | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/mapHelpers.ts b/src/mapHelpers.ts index d7a9ac4c..be8472b0 100644 --- a/src/mapHelpers.ts +++ b/src/mapHelpers.ts @@ -1,4 +1,4 @@ -import { ComponentInstance } from '@vue/devtools-api' +import { ComponentPublicInstance } from 'vue' import { GenericStore, GettersTree, @@ -52,10 +52,10 @@ function getCachedStore< G extends GettersTree = GettersTree, A = Record >( - vm: ComponentInstance, + vm: ComponentPublicInstance, useStore: StoreDefinition ): Store { - const cache = '_pStores' in vm ? vm._pStores : (vm._pStores = {}) + const cache = '_pStores' in vm ? vm._pStores! : (vm._pStores = {}) const id = useStore.$id return (cache[id] || (cache[id] = useStore(vm.$pinia))) as Store } @@ -248,13 +248,13 @@ export function mapState< ): MapStateReturn | MapStateObjectReturn { return Array.isArray(keysOrMapper) ? keysOrMapper.reduce((reduced, key) => { - reduced[key] = function (this: ComponentInstance) { + reduced[key] = function (this: ComponentPublicInstance) { return getCachedStore(this, useStore)[key] } as () => any return reduced }, {} as MapStateReturn) : Object.keys(keysOrMapper).reduce((reduced, key: keyof KeyMapper) => { - reduced[key] = function (this: ComponentInstance) { + reduced[key] = function (this: ComponentPublicInstance) { const store = getCachedStore(this, useStore) const storeKey = keysOrMapper[key] // for some reason TS is unable to infer the type of storeKey to be a @@ -368,13 +368,19 @@ export function mapActions< ): MapActionsReturn | MapActionsObjectReturn { return Array.isArray(keysOrMapper) ? keysOrMapper.reduce((reduced, key) => { - reduced[key] = function (this: ComponentInstance, ...args: any[]) { + reduced[key] = function ( + this: ComponentPublicInstance, + ...args: any[] + ) { return (getCachedStore(this, useStore)[key] as Method)(...args) } as Store[keyof A] return reduced }, {} as MapActionsReturn) : Object.keys(keysOrMapper).reduce((reduced, key: keyof KeyMapper) => { - reduced[key] = function (this: ComponentInstance, ...args: any[]) { + reduced[key] = function ( + this: ComponentPublicInstance, + ...args: any[] + ) { return getCachedStore(this, useStore)[keysOrMapper[key]](...args) } as Store[keyof KeyMapper[]] return reduced @@ -455,10 +461,10 @@ export function mapWritableState< ? keysOrMapper.reduce((reduced, key) => { // @ts-ignore reduced[key] = { - get(this: ComponentInstance) { + get(this: ComponentPublicInstance) { return getCachedStore(this, useStore)[key] }, - set(value) { + set(this: ComponentPublicInstance, value) { // it's easier to type it here as any return (getCachedStore(this, useStore)[key] = value as any) }, @@ -468,10 +474,10 @@ export function mapWritableState< : Object.keys(keysOrMapper).reduce((reduced, key: keyof KeyMapper) => { // @ts-ignore reduced[key] = { - get(this: ComponentInstance) { + get(this: ComponentPublicInstance) { return getCachedStore(this, useStore)[keysOrMapper[key]] }, - set(value) { + set(this: ComponentPublicInstance, value) { // it's easier to type it here as any return (getCachedStore(this, useStore)[ keysOrMapper[key] -- 2.47.3