From c25cbdb9e41afa26dcfbc4a3bd462d99b5e92324 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Wed, 28 Jul 2021 11:06:24 +0200 Subject: [PATCH] types: allow cast from Store to StoreGeneric --- src/devtools/plugin.ts | 7 ++-- src/store.ts | 2 - src/types.ts | 95 ++++++++++++++++++++++-------------------- yarn.lock | 6 +-- 4 files changed, 55 insertions(+), 55 deletions(-) diff --git a/src/devtools/plugin.ts b/src/devtools/plugin.ts index 36333d36..2bf932d9 100644 --- a/src/devtools/plugin.ts +++ b/src/devtools/plugin.ts @@ -429,8 +429,7 @@ export function devtoolsPlugin< toRaw(store)._hotUpdate = function (newStore) { originalHotUpdate.apply(this, arguments as any) patchActionForGrouping( - // @ts-expect-error: can cast the store... - store, + store as StoreGeneric, Object.keys(newStore._hmrPayload.actions) ) } @@ -438,7 +437,7 @@ export function devtoolsPlugin< addStoreToDevtools( app, - // @ts-expect-error: FIXME: if possible... - store + // FIXME: is there a way to allow the assignment from Store to StoreGeneric? + store as StoreGeneric ) } diff --git a/src/store.ts b/src/store.ts index 9a2cde45..3672c9bd 100644 --- a/src/store.ts +++ b/src/store.ts @@ -530,7 +530,6 @@ function createSetupStore< pinia._p.forEach((extender) => { if (__DEV__ && IS_CLIENT) { const extensions = extender({ - // @ts-expect-error: conflict between A and ActionsTree store, app: pinia._a, pinia, @@ -545,7 +544,6 @@ function createSetupStore< assign( store, extender({ - // @ts-expect-error: conflict between A and ActionsTree store, app: pinia._a, pinia, diff --git a/src/types.ts b/src/types.ts index 67ea1055..1a4593cf 100644 --- a/src/types.ts +++ b/src/types.ts @@ -6,14 +6,6 @@ import { Pinia } from './rootStore' */ export type StateTree = Record -/** - * Object descriptor for Object.defineProperty - */ -export interface StateDescriptor { - get(): S - set(newValue: S): void -} - export function isPlainObject( // eslint-disable-next-line @typescript-eslint/no-explicit-any o: any @@ -157,6 +149,48 @@ export type SubscriptionCallback = ( state: UnwrapRef ) => void +type _StoreOnActionListenerContext = { + /** + * Name of the action + */ + name: ActionName + + /** + * Store that is invoking the action + */ + store: Store + + /** + * Parameters passed to the action + */ + args: A extends Record + ? Parameters + : unknown[] + + /** + * Sets up a hook once the action is finished. It receives the return value + * of the action, if it's a Promise, it will be unwrapped. Can return a + * value (other than `undefined`) to **override** the returned value. + */ + after: ( + callback: A extends Record + ? ( + resolvedReturn: UnwrapPromise> + // allow the after callback to override the return value + ) => + | void + | ReturnType + | UnwrapPromise> + : () => void + ) => void + + /** + * Sets up a hook if the action fails. Return `false` to catch the error and + * stop it fro propagating. + */ + onError: (callback: (error: unknown) => unknown | false) => void +} + /** * Context object passed to callbacks of `store.$onAction(context => {})` * TODO: should have only the Id, the Store and Actions to generate the proper object @@ -166,44 +200,13 @@ export type StoreOnActionListenerContext< S extends StateTree, G /* extends GettersTree */, A /* extends ActionsTree */ -> = { - [Name in keyof A]: { - /** - * Name of the action - */ - name: Name - - /** - * Store that is invoking the action - */ - store: ActionsTree extends A ? StoreGeneric : Store - - /** - * Parameters passed to the action - */ - args: A[Name] extends _Method ? Parameters : unknown[] - - /** - * Sets up a hook once the action is finished. It receives the return value - * of the action, if it's a Promise, it will be unwrapped. Can return a - * value (other than `undefined`) to **override** the returned value. - */ - after: ( - callback: A[Name] extends _Method - ? ( - resolvedReturn: UnwrapPromise> - // allow the after callback to override the return value - ) => void | ReturnType | UnwrapPromise> - : () => void - ) => void - - /** - * Sets up a hook if the action fails. Return `false` to catch the error and - * stop it fro propagating. - */ - onError: (callback: (error: unknown) => unknown | false) => void - } -}[keyof A] +> = ActionsTree extends A + ? _StoreOnActionListenerContext + : { + [Name in keyof A]: Name extends string + ? _StoreOnActionListenerContext, Name, A> + : never + }[keyof A] /** * Argument of `store.$onAction()` diff --git a/yarn.lock b/yarn.lock index ffeaec4d..d47067d2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1211,9 +1211,9 @@ integrity sha512-P00v895ONlx4P6D8p9OyJv+iL0+QghaDm1BDlhP8ibsu9MqHznoyZ/r1rHuLQEAR//4SMMo/9dC3SW8edUPvBw== "@vue/test-utils@^2.0.0-rc.10": - version "2.0.0-rc.11" - resolved "https://registry.yarnpkg.com/@vue/test-utils/-/test-utils-2.0.0-rc.11.tgz#62521dc1d758cb0458a375e7eba1a4503e1e27f5" - integrity sha512-7XMp3ha/dSvPoJAWx9Ils9CLByu8Ntk/DOPXj8Elp/fqtUXngj/DiXEcBBNLboiS5ux3xYfKfakzz4JNpind+A== + version "2.0.0-rc.12" + resolved "https://registry.yarnpkg.com/@vue/test-utils/-/test-utils-2.0.0-rc.12.tgz#716a84d915d6045640eeac416cc2a2acd514e06e" + integrity sha512-G9BGRYlfwWjhorGjnpniC3hcYn1pCG2NqKG68fdUpk3DgWKordZ+BsEFD/SAmKdTZVMCY1huFwY3XAbPc+AgRw== "@vueuse/core@^5.2.0": version "5.2.0" -- 2.47.2