From 42497d88aa3205fda310f7621250dc210d4ef520 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Fri, 23 Jul 2021 13:53:38 +0200 Subject: [PATCH] refactor(types): add DefineStoreOptionsBase --- src/index.ts | 1 + src/store.ts | 1 + src/types.ts | 82 +++++++++++++++++++++++++++++++--------------------- 3 files changed, 51 insertions(+), 33 deletions(-) diff --git a/src/index.ts b/src/index.ts index 7b801247..b9c8c21e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -26,6 +26,7 @@ export type { _SubscriptionCallbackMutationBase, PiniaCustomProperties, PiniaCustomStateProperties, + DefineStoreOptionsBase, DefineStoreOptions, DefineSetupStoreOptions, DefineStoreOptionsInPlugin, diff --git a/src/store.ts b/src/store.ts index 14d324d7..1b070915 100644 --- a/src/store.ts +++ b/src/store.ts @@ -558,6 +558,7 @@ function createSetupStore< }) if (initialState) { + // @ts-expect-error: initialState doesn't match ;(options.hydrate || innerPatch)(store, initialState) } diff --git a/src/types.ts b/src/types.ts index 7367e5e4..78a608a3 100644 --- a/src/types.ts +++ b/src/types.ts @@ -159,6 +159,7 @@ export type SubscriptionCallback = ( /** * Context object passed to callbacks of `store.$onAction(context => {})` + * TODO: should have only the Id, the Store and Actions to generate the proper object */ export type StoreOnActionListenerContext< Id extends string, @@ -223,25 +224,14 @@ export type StoreOnActionListener< ) => void /** - * Base store with state and functions - * @internal + * Properties of a store. */ -export interface StoreWithState< - Id extends string, - S extends StateTree, - G /* extends GettersTree */, - A /* extends ActionsTree */ -> { +export interface StoreProperties { /** * Unique identifier of the store */ $id: Id - /** - * State of the Store. Setting it will replace the whole state. - */ - $state: UnwrapRef & PiniaCustomStateProperties - /** * Private property defining the pinia the store is attached to. * @@ -279,10 +269,26 @@ export interface StoreWithState< */ _hmrPayload: { state: string[] - hotState: Ref + hotState: Ref actions: ActionsTree getters: ActionsTree } +} + +/** + * Base store with state and functions + * @internal + */ +export interface StoreWithState< + Id extends string, + S extends StateTree, + G /* extends GettersTree */, + A /* extends ActionsTree */ +> extends StoreProperties { + /** + * State of the Store. Setting it will replace the whole state. + */ + $state: UnwrapRef & PiniaCustomStateProperties /** * Applies a state patch to current state. Allows passing nested values @@ -305,6 +311,7 @@ export interface StoreWithState< /** * Resets the store to its initial state by building a new state object. + * TODO: make this options only */ $reset(): void @@ -410,7 +417,7 @@ export type Store< UnwrapRef & StoreWithGetters & // StoreWithActions & - (ActionsTree extends A ? {} : StoreWithActions) & + (ActionsTree extends A ? {} : A) & PiniaCustomProperties & PiniaCustomStateProperties /** @@ -440,7 +447,7 @@ export type GenericStore< > = StoreWithState & UnwrapRef & StoreWithGetters & - StoreWithActions & + A & PiniaCustomProperties & PiniaCustomStateProperties @@ -513,15 +520,31 @@ export type GettersTree = Record< export type ActionsTree = Record /** - * Options parameter of `defineStore()`. Can be extended to augment stores with - * the plugin API. + * Options passed to `defineStore()` that are common between option and setup + * stores. Extend this interface if you want to add custom options to both kinds + * of stores. + */ +export interface DefineStoreOptionsBase { + /** + * Allows hydrating the store during SSR when there is an available state in + * pinia.state. + * + * @param store - the store + * @param initialState - initialState + */ + hydrate?(store: Store, initialState: UnwrapRef): void +} + +/** + * Options parameter of `defineStore()` for option stores. Can be extended to + * augment stores with the plugin API. {@see DefineStoreOptionsBase}. */ export interface DefineStoreOptions< Id extends string, S extends StateTree, G /* extends GettersTree */, A /* extends Record */ -> { +> extends DefineStoreOptionsBase, S> { /** * Unique string key to identify the store across the application. */ @@ -550,26 +573,19 @@ export interface DefineStoreOptions< StoreWithGetters & PiniaCustomProperties > - - /** - * Allows hydrating the store during SSR when there is an available state in - * pinia.state. - * - * @param store - the store - * @param initialState - initialState - */ - hydrate?(store: Store, initialState: UnwrapRef): void } +/** + * Options parameter of `defineStore()` for setup stores. Can be extended to + * augment stores with the plugin API. {@see DefineStoreOptionsBase}. + */ export interface DefineSetupStoreOptions< Id extends string, + // TODO: pass SS instead S extends StateTree, G, A /* extends ActionsTree */ -> extends Omit< - DefineStoreOptions, - 'actions' | 'id' | 'state' | 'getters' - > { +> extends DefineStoreOptionsBase, S> { /** * Extracted actions. Added by useStore(). SHOULD NOT be added by the user when * creating the store. Can be used in plugins to get the list of actions in a @@ -586,7 +602,7 @@ export interface DefineStoreOptionsInPlugin< S extends StateTree, G, A -> extends Omit, 'id'> { +> extends Omit, 'id' | 'actions'> { /** * Extracted object of actions. Added by useStore() when the store is built * using the setup API, otherwise uses the one passed to `defineStore()`. -- 2.47.2