]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
types: allow cast from Store to StoreGeneric
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 28 Jul 2021 09:06:24 +0000 (11:06 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 28 Jul 2021 09:06:24 +0000 (11:06 +0200)
src/devtools/plugin.ts
src/store.ts
src/types.ts
yarn.lock

index 36333d36e44bd97b6c499532b9b3ed8e0c873552..2bf932d9d25e740e4792349c4379527b59376167 100644 (file)
@@ -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<Id, S, G, A> to StoreGeneric?
+    store as StoreGeneric
   )
 }
index 9a2cde4528416847f00c6bfaf7a6911d66b35583..3672c9bd83dc1962520136cb473aa2b543ea7205 100644 (file)
@@ -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,
index 67ea1055f8eedf9102746bc38253ce5ae9cc6822..1a4593cfbbb4954a04d38e406d225156a0101987 100644 (file)
@@ -6,14 +6,6 @@ import { Pinia } from './rootStore'
  */
 export type StateTree = Record<string | number | symbol, any>
 
-/**
- * Object descriptor for Object.defineProperty
- */
-export interface StateDescriptor<S extends StateTree = {}> {
-  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<S> = (
   state: UnwrapRef<S>
 ) => void
 
+type _StoreOnActionListenerContext<Store, ActionName extends string, A> = {
+  /**
+   * Name of the action
+   */
+  name: ActionName
+
+  /**
+   * Store that is invoking the action
+   */
+  store: Store
+
+  /**
+   * Parameters passed to the action
+   */
+  args: A extends Record<ActionName, _Method>
+    ? Parameters<A[ActionName]>
+    : 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<ActionName, _Method>
+      ? (
+          resolvedReturn: UnwrapPromise<ReturnType<A[ActionName]>>
+          // allow the after callback to override the return value
+        ) =>
+          | void
+          | ReturnType<A[ActionName]>
+          | UnwrapPromise<ReturnType<A[ActionName]>>
+      : () => 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<S> */,
   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<Id, S, G, A>
-
-    /**
-     * Parameters passed to the action
-     */
-    args: A[Name] extends _Method ? Parameters<A[Name]> : 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<ReturnType<A[Name]>>
-            // allow the after callback to override the return value
-          ) => void | ReturnType<A[Name]> | UnwrapPromise<ReturnType<A[Name]>>
-        : () => 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<StoreGeneric, string, ActionsTree>
+  : {
+      [Name in keyof A]: Name extends string
+        ? _StoreOnActionListenerContext<Store<Id, S, G, A>, Name, A>
+        : never
+    }[keyof A]
 
 /**
  * Argument of `store.$onAction()`
index ffeaec4d8cf12cf7b0228d97e3cf37caf1e3d1c6..d47067d23e6ad4ae43e3317dfb9f34fe368eba65 100644 (file)
--- a/yarn.lock
+++ b/yarn.lock
   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"