Skip to content

Commit d71d89f

Browse files
committed
refactor(types): move extractor types
1 parent 3985ee8 commit d71d89f

File tree

2 files changed

+62
-61
lines changed

2 files changed

+62
-61
lines changed

src/store.ts

Lines changed: 3 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ import {
3737
DefineStoreOptionsInPlugin,
3838
StoreGeneric,
3939
StoreWithGetters,
40+
_ExtractActionsFromSetupStore,
41+
_ExtractGettersFromSetupStore,
42+
_ExtractStateFromSetupStore,
4043
} from './types'
4144
import {
4245
getActivePinia,
@@ -132,8 +135,6 @@ function createOptionsStore<
132135

133136
store = createSetupStore(id, setup, options, pinia, hot)
134137

135-
// TODO: HMR should also replace getters here
136-
137138
store.$reset = $reset
138139

139140
return store as any
@@ -574,63 +575,6 @@ function createSetupStore<
574575

575576
// }
576577

577-
/**
578-
* @internal
579-
*/
580-
type _SpreadStateFromStore<SS, K extends readonly any[]> = K extends readonly [
581-
infer A,
582-
...infer Rest
583-
]
584-
? A extends string | number | symbol
585-
? SS extends Record<A, _Method | ComputedRef<any>>
586-
? _SpreadStateFromStore<SS, Rest>
587-
: SS extends Record<A, any>
588-
? Record<A, UnwrapRef<SS[A]>> & _SpreadStateFromStore<SS, Rest>
589-
: never
590-
: {}
591-
: {}
592-
593-
/**
594-
* @internal
595-
*/
596-
type _SpreadPropertiesFromObject<
597-
SS,
598-
K extends readonly any[],
599-
T
600-
> = K extends readonly [infer A, ...infer Rest]
601-
? A extends string | number | symbol
602-
? SS extends Record<A, T>
603-
? Record<A, UnwrapRef<SS[A]>> & _SpreadPropertiesFromObject<SS, Rest, T>
604-
: _SpreadPropertiesFromObject<SS, Rest, T>
605-
: {}
606-
: {}
607-
608-
/**
609-
* @internal
610-
*/
611-
type _ExtractStateFromSetupStore<SS> = _SpreadStateFromStore<
612-
SS,
613-
_UnionToTuple<keyof SS>
614-
>
615-
616-
/**
617-
* @internal
618-
*/
619-
type _ExtractActionsFromSetupStore<SS> = _SpreadPropertiesFromObject<
620-
SS,
621-
_UnionToTuple<keyof SS>,
622-
_Method
623-
>
624-
625-
/**
626-
* @internal
627-
*/
628-
type _ExtractGettersFromSetupStore<SS> = _SpreadPropertiesFromObject<
629-
SS,
630-
_UnionToTuple<keyof SS>,
631-
ComputedRef<any>
632-
>
633-
634578
/**
635579
* Extract the actions of a store type. Works with both a Setup Store or an
636580
* Options Store.

src/types.ts

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DebuggerEvent, Ref, UnwrapRef } from 'vue'
1+
import { ComputedRef, DebuggerEvent, Ref, UnwrapRef } from 'vue'
22
import { Pinia } from './rootStore'
33

44
/**
@@ -549,6 +549,63 @@ export interface DefineStoreOptionsBase<S extends StateTree, Store> {
549549
hydrate?(store: Store, initialState: UnwrapRef<S>): void
550550
}
551551

552+
/**
553+
* @internal
554+
*/
555+
type _SpreadStateFromStore<SS, K extends readonly any[]> = K extends readonly [
556+
infer A,
557+
...infer Rest
558+
]
559+
? A extends string | number | symbol
560+
? SS extends Record<A, _Method | ComputedRef<any>>
561+
? _SpreadStateFromStore<SS, Rest>
562+
: SS extends Record<A, any>
563+
? Record<A, UnwrapRef<SS[A]>> & _SpreadStateFromStore<SS, Rest>
564+
: never
565+
: {}
566+
: {}
567+
568+
/**
569+
* @internal
570+
*/
571+
type _SpreadPropertiesFromObject<
572+
SS,
573+
K extends readonly any[],
574+
T
575+
> = K extends readonly [infer A, ...infer Rest]
576+
? A extends string | number | symbol
577+
? SS extends Record<A, T>
578+
? Record<A, UnwrapRef<SS[A]>> & _SpreadPropertiesFromObject<SS, Rest, T>
579+
: _SpreadPropertiesFromObject<SS, Rest, T>
580+
: {}
581+
: {}
582+
583+
/**
584+
* @internal
585+
*/
586+
export type _ExtractStateFromSetupStore<SS> = _SpreadStateFromStore<
587+
SS,
588+
_UnionToTuple<keyof SS>
589+
>
590+
591+
/**
592+
* @internal
593+
*/
594+
export type _ExtractActionsFromSetupStore<SS> = _SpreadPropertiesFromObject<
595+
SS,
596+
_UnionToTuple<keyof SS>,
597+
_Method
598+
>
599+
600+
/**
601+
* @internal
602+
*/
603+
export type _ExtractGettersFromSetupStore<SS> = _SpreadPropertiesFromObject<
604+
SS,
605+
_UnionToTuple<keyof SS>,
606+
ComputedRef<any>
607+
>
608+
552609
/**
553610
* Options parameter of `defineStore()` for option stores. Can be extended to
554611
* augment stores with the plugin API. @see {@link DefineStoreOptionsBase}.
@@ -595,7 +652,7 @@ export interface DefineStoreOptions<
595652
*/
596653
export interface DefineSetupStoreOptions<
597654
Id extends string,
598-
// TODO: pass SS instead
655+
// NOTE: Passing SS seems to make TS crash
599656
S extends StateTree,
600657
G,
601658
A /* extends ActionsTree */

0 commit comments

Comments
 (0)