Skip to content

Commit

Permalink
feat(types): SetupStoreDefinition
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Oct 13, 2023
1 parent 49924c8 commit 391f9ac
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
7 changes: 6 additions & 1 deletion packages/pinia/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ export type {
} from './rootStore'

export { defineStore, skipHydrate } from './store'
export type { StoreActions, StoreGetters, StoreState } from './store'
export type {
StoreActions,
StoreGetters,
StoreState,
SetupStoreDefinition,
} from './store'

export type {
StateTree,
Expand Down
14 changes: 14 additions & 0 deletions packages/pinia/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -964,3 +964,17 @@ export function defineStore(

return useStore
}

/**
* Return type of `defineStore()` with a setup function.
* - `Id` is a string literal of the store's name
* - `SS` is the return type of the setup function
* @see {@link StoreDefinition}
*/
export interface SetupStoreDefinition<Id extends string, SS>
extends StoreDefinition<
Id,
_ExtractStateFromSetupStore<SS>,
_ExtractGettersFromSetupStore<SS>,
_ExtractActionsFromSetupStore<SS>
> {}
33 changes: 25 additions & 8 deletions packages/pinia/test-dts/typeHelpers.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { StoreDefinition } from './'
import { computed, ref } from 'vue'
import { ComputedRef, Ref, computed, ref } from 'vue'
import {
StoreDefinition,
SetupStoreDefinition,
StoreState,
StoreGetters,
StoreActions,
Expand Down Expand Up @@ -60,10 +61,26 @@ expectType<{ n: number }>(storeState(useOptionsStore))

expectType<{ double: number }>(storeGetters(useOptionsStore))

expectType<{ n: number }>(
storeState(
defineStore('', {
state: () => ({ n: ref(0) }),
})
)
expectType<
SetupStoreDefinition<
'a',
{
n: Ref<number>
double: ComputedRef<number>
increment: () => void
}
>
>(
defineStore('a', () => {
const n = ref(0)
const double = computed(() => n.value * 2)
function increment() {
n.value++
}
return {
double,
increment,
n,
}
})
)

0 comments on commit 391f9ac

Please sign in to comment.