]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
feat(types): fail on async patch
authorEduardo San Martin Morote <posva13@gmail.com>
Fri, 9 Apr 2021 18:35:57 +0000 (20:35 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Fri, 9 Apr 2021 18:35:57 +0000 (20:35 +0200)
src/types.ts
test-dts/store.test-d.ts

index bc81a256ecd0c884c4ec98e9792d469ba3002aa1..a7a5a2a410b54ed8b1d3a647eaf14b468f94ad3b 100644 (file)
@@ -76,9 +76,12 @@ export interface StoreWithState<Id extends string, S extends StateTree> {
    * Sets or arrays and applying an object patch isn't practical, e.g. appending
    * to an array.
    *
-   * @param stateMutator - function that mutates `state`
+   * @param stateMutator - function that mutates `state`, cannot be async
    */
-  $patch(stateMutator: (state: S) => void): void
+  $patch<F extends (state: S) => void>(
+    // this prevents the user from using `async` which isn't allowed
+    stateMutator: ReturnType<F> extends Promise<any> ? never : F
+  ): void
 
   /**
    * Resets the store to its initial state by building a new state object.
index 8105e2f7cfb54cbcdeb5937fe0e2c639cb43d548..5350092be18e3ba28f815f1728a882614034add9 100644 (file)
@@ -21,3 +21,11 @@ store.nonExistant
 
 // @ts-expect-error
 store.nonExistant.stuff
+
+// @ts-expect-error cannot return a value
+store.$patch(async () => {})
+store.$patch(() => {})
+store.$patch(() => {
+  // return earlier
+  return
+})