]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
feat: wip pass state to getters
authorEduardo San Martin Morote <posva13@gmail.com>
Tue, 27 Apr 2021 12:52:27 +0000 (14:52 +0200)
committerEduardo San Martin Morote <posva@users.noreply.github.com>
Sun, 2 May 2021 15:45:55 +0000 (17:45 +0200)
__tests__/getters.spec.ts
__tests__/storePlugins.spec.ts
src/store.ts
src/types.ts
test-dts/store.test-d.ts

index 28352989dd5952b3b5548a2c17718599928e7424..270d756cf4d20863d7f5d166dd8f480934246f16 100644 (file)
@@ -19,6 +19,19 @@ describe('Getters', () => {
         composed(): string {
           return this.upperCaseName + ': ok'
         },
+        arrowUpper: (state) => {
+          // @ts-expect-error
+          state.nope
+          state.name.toUpperCase()
+        },
+      },
+      actions: {
+        o() {
+          // @ts-expect-error it should type getters
+          this.arrowUper.toUpperCase()
+          this.o().toUpperCase()
+          return 'a string'
+        },
       },
     })()
   }
@@ -42,6 +55,10 @@ describe('Getters', () => {
   it('adds getters to the store', () => {
     const store = useStore()
     expect(store.upperCaseName).toBe('EDUARDO')
+
+    // @ts-expect-error
+    store.nope
+
     store.name = 'Ed'
     expect(store.upperCaseName).toBe('ED')
   })
index ef75b81e3534f43ce05ea1682c9b5df52ce54b7a..d93f354ec31dbedbacad1a73e84bb156c4672958 100644 (file)
@@ -17,6 +17,8 @@ describe('store plugins', () => {
   const useStore = defineStore({
     id: 'test',
 
+    state: () => ({ n: 0 }),
+
     actions: {
       incrementN() {
         return this.n++
index 73060a8af8e989b3f6c2adab4f5c3163c58d5537..21673567a2b243ec668805a793da48b9b4664d10 100644 (file)
@@ -289,9 +289,8 @@ let isDevWarned: boolean | undefined
 export function defineStore<
   Id extends string,
   S extends StateTree,
-  // the omission of the extends is necessary for type inference
   G extends GettersTree<S>,
-  A /* extends Record<string, Method> */
+  A /* extends Record<string, StoreAction> */
 >(options: DefineStoreOptions<Id, S, G, A>): StoreDefinition<Id, S, G, A> {
   const { id, state, getters, actions } = options
 
index e88180363eee150de198a0988755292d9d88bb0a..ca6cfba456d4813706ee3e4a2dc6588181d3fdf8 100644 (file)
@@ -26,14 +26,6 @@ export function isPlainObject(
   )
 }
 
-/**
- * Store Getter
- * @internal
- */
-export interface StoreGetter<S extends StateTree, T = any> {
-  (state: S, getters: Record<string, Ref<any>>): T
-}
-
 export type DeepPartial<T> = { [K in keyof T]?: DeepPartial<T[K]> }
 // type DeepReadonly<T> = { readonly [P in keyof T]: DeepReadonly<T[P]> }
 
@@ -129,12 +121,7 @@ export type StoreWithActions<A> = {
  * @internal
  */
 export type StoreWithGetters<G> = {
-  readonly [k in keyof G]: G[k] extends (
-    this: infer This,
-    store?: any
-  ) => infer R
-    ? R
-    : never
+  readonly [k in keyof G]: G[k] extends (...args: any[]) => infer R ? R : never
 }
 
 // // in this type we forget about this because otherwise the type is recursive
index cabd1f7f8bc70fd00759d2504334505512f7b634..8db5bb85e9bfc8255bb23726e0cddc4327da4efa 100644 (file)
@@ -16,6 +16,11 @@ const useStore = defineStore({
       expectType<string>(this.upper)
       return false
     },
+
+    doubleCounter: (state) => {
+      expectType<number>(state.nested.counter)
+      return state.nested.counter * 2
+    },
   },
   actions: {
     doStuff() {