From d479370f2127f09fb2077c5fc54c941d7db5e91d Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Wed, 28 Jul 2021 14:45:25 +0200 Subject: [PATCH] fix(types): correct order for hydrate function --- docs/core-concepts/plugins.md | 18 ++++++++++++++++ src/store.ts | 1 - src/types.ts | 4 ++-- test-dts/customizations.test-d.ts | 35 ++++++++++++++++++++++++------- 4 files changed, 47 insertions(+), 11 deletions(-) diff --git a/docs/core-concepts/plugins.md b/docs/core-concepts/plugins.md index 703976f2..00e56563 100644 --- a/docs/core-concepts/plugins.md +++ b/docs/core-concepts/plugins.md @@ -221,6 +221,24 @@ pinia.use(({ options, store }) => { }) ``` +Note that custom options are passed as the 3rd argument when using the setup syntax: + +```js +defineStore( + 'search', + () => { + // ... + }, + { + // this will be read by a plugin later on + debounce: { + // debounce the action searchContacts by 300ms + searchContacts: 300, + }, + } +) +``` + ## TypeScript Everything shown above can be done with typing support, so you don't ever need to use `any` or `@ts-ignore`. diff --git a/src/store.ts b/src/store.ts index 6c0d1d1b..e69e779b 100644 --- a/src/store.ts +++ b/src/store.ts @@ -561,7 +561,6 @@ 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 c552a372..7a38a2e5 100644 --- a/src/types.ts +++ b/src/types.ts @@ -550,7 +550,7 @@ export interface DefineStoreOptions< S extends StateTree, G /* extends GettersTree */, A /* extends Record */ -> extends DefineStoreOptionsBase, S> { +> extends DefineStoreOptionsBase> { /** * Unique string key to identify the store across the application. */ @@ -591,7 +591,7 @@ export interface DefineSetupStoreOptions< S extends StateTree, G, A /* extends ActionsTree */ -> extends DefineStoreOptionsBase, S> { +> extends DefineStoreOptionsBase> { /** * 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 diff --git a/test-dts/customizations.test-d.ts b/test-dts/customizations.test-d.ts index 71746081..a60c14f6 100644 --- a/test-dts/customizations.test-d.ts +++ b/test-dts/customizations.test-d.ts @@ -25,12 +25,13 @@ declare module '../dist/pinia' { stateOnly: number } - export interface DefineStoreOptions { - debounce?: { - // Record - [k in keyof A]?: number - } + export interface DefineStoreOptionsBase { + debounce?: Partial, number>> } + + // export interface DefineStoreOptions { + // debounce?: Partial> + // } } const pinia = createPinia() @@ -89,6 +90,23 @@ const useStore = defineStore({ }, }) +defineStore( + 'withSetup', + () => { + function one() {} + function two() {} + function three() {} + + return { one, two, three } + }, + { + debounce: { + one: 200, + two: 300, + }, + } +) + type Procedure = (...args: any[]) => any function debounce(fn: F, time: number = 200) { @@ -100,11 +118,12 @@ expectType<{ }>(mapStores(useStore)) pinia.use(({ options, store }) => { - if (options.debounce) { - return Object.keys(options.debounce).reduce((debouncedActions, action) => { + const { debounce: debounceOptions } = options + if (debounceOptions) { + return Object.keys(debounceOptions).reduce((debouncedActions, action) => { debouncedActions[action] = debounce( store[action], - options.debounce![action as keyof typeof options['actions']] + debounceOptions[action] ) return debouncedActions }, {} as Record any>) -- 2.47.2