]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
feat(types): allow defining custom state properties
authorEduardo San Martin Morote <posva13@gmail.com>
Sat, 15 May 2021 15:15:02 +0000 (17:15 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Sat, 15 May 2021 15:15:18 +0000 (17:15 +0200)
src/index.ts
src/rootStore.ts
src/types.ts
test-dts/customizations.test-d.ts

index 27892b70879dd29a4a35688b7cb62494287c0ce8..7c577382a043cb56b030b5ac841ad66ff7d46d64 100644 (file)
@@ -18,6 +18,7 @@ export type {
   StoreOnActionListenerContext,
   SubscriptionCallback,
   PiniaCustomProperties,
+  PiniaCustomStateProperties,
   DefineStoreOptions,
 } from './types'
 export { MutationType } from './types'
index e991852bbe7bcee00a8af13d80cfa0d4edd54ff9..37dc3dd471cdb6b68ac7a3322a3ae8cc1829de2c 100644 (file)
@@ -9,6 +9,7 @@ import {
   Store,
   GettersTree,
   ActionsTree,
+  PiniaCustomStateProperties,
 } from './types'
 
 /**
@@ -153,5 +154,7 @@ export interface PiniaStorePlugin {
    *
    * @param context - Context
    */
-  (context: PiniaPluginContext): Partial<PiniaCustomProperties> | void
+  (context: PiniaPluginContext): Partial<
+    PiniaCustomProperties & PiniaCustomStateProperties
+  > | void
 }
index 66c4d6f050bb47c9010b58b948d44677ad4f978e..b83fa2c348fe3f8620df5060e967904621725584 100644 (file)
@@ -231,7 +231,7 @@ export interface StoreWithState<
   /**
    * State of the Store. Setting it will replace the whole state.
    */
-  $state: UnwrapRef<S>
+  $state: UnwrapRef<S> & PiniaCustomStateProperties<S>
 
   /**
    * Private property defining the pinia the store is attached to.
@@ -380,7 +380,8 @@ export type Store<
   UnwrapRef<S> &
   StoreWithGetters<G> &
   StoreWithActions<A> &
-  PiniaCustomProperties<Id, S, G, A>
+  PiniaCustomProperties<Id, S, G, A> &
+  PiniaCustomStateProperties<S>
 
 /**
  * Return type of `defineStore()`. Function that allows instantiating a store.
@@ -420,6 +421,11 @@ export interface PiniaCustomProperties<
   A /* extends ActionsTree */ = ActionsTree
 > {}
 
+/**
+ * Properties that are added to every `store.$state` by `pinia.use()`
+ */
+export interface PiniaCustomStateProperties<S extends StateTree = StateTree> {}
+
 /**
  * Type of an object of Getters that infers the argument
  *
@@ -427,7 +433,7 @@ export interface PiniaCustomProperties<
  */
 export type GettersTree<S extends StateTree> = Record<
   string,
-  ((state: UnwrapRef<S>) => any) | (() => any)
+  ((state: UnwrapRef<S & PiniaCustomStateProperties<S>>) => any) | (() => any)
 >
 
 /**
index c4aaf6d4322b32fb0e840f79c7e27592e33011a9..e37c4972a148537fcdb9a53c6bafec4d179b33b5 100644 (file)
@@ -11,6 +11,10 @@ declare module '../dist/pinia' {
     $actions: Array<keyof A>
   }
 
+  export interface PiniaCustomStateProperties<S> {
+    myState: number
+  }
+
   export interface DefineStoreOptions<Id, S, G, A> {
     debounce?: {
       // Record<keyof A, number>
@@ -26,6 +30,9 @@ pinia.use((context) => {
   expectType<string>(context.store.$id)
   expectType<App>(context.app)
 
+  expectType<number>(context.store.$state.myState)
+  expectType<number>(context.store.myState)
+
   return {
     $actions: Object.keys(context.options.actions || {}),
   }
@@ -33,10 +40,13 @@ pinia.use((context) => {
 
 const useStore = defineStore({
   id: 'main',
+  state: () => ({}),
   actions: {
     one() {},
     two() {
       this.one()
+      expectType<number>(this.$state.myState)
+      expectType<number>(this.myState)
     },
     three() {
       this.two()