Skip to content

Commit

Permalink
refactor(types): move extractor types
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Jul 28, 2021
1 parent 3985ee8 commit d71d89f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 61 deletions.
62 changes: 3 additions & 59 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ import {
DefineStoreOptionsInPlugin,
StoreGeneric,
StoreWithGetters,
_ExtractActionsFromSetupStore,
_ExtractGettersFromSetupStore,
_ExtractStateFromSetupStore,
} from './types'
import {
getActivePinia,
Expand Down Expand Up @@ -132,8 +135,6 @@ function createOptionsStore<

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

// TODO: HMR should also replace getters here

store.$reset = $reset

return store as any
Expand Down Expand Up @@ -574,63 +575,6 @@ function createSetupStore<

// }

/**
* @internal
*/
type _SpreadStateFromStore<SS, K extends readonly any[]> = K extends readonly [
infer A,
...infer Rest
]
? A extends string | number | symbol
? SS extends Record<A, _Method | ComputedRef<any>>
? _SpreadStateFromStore<SS, Rest>
: SS extends Record<A, any>
? Record<A, UnwrapRef<SS[A]>> & _SpreadStateFromStore<SS, Rest>
: never
: {}
: {}

/**
* @internal
*/
type _SpreadPropertiesFromObject<
SS,
K extends readonly any[],
T
> = K extends readonly [infer A, ...infer Rest]
? A extends string | number | symbol
? SS extends Record<A, T>
? Record<A, UnwrapRef<SS[A]>> & _SpreadPropertiesFromObject<SS, Rest, T>
: _SpreadPropertiesFromObject<SS, Rest, T>
: {}
: {}

/**
* @internal
*/
type _ExtractStateFromSetupStore<SS> = _SpreadStateFromStore<
SS,
_UnionToTuple<keyof SS>
>

/**
* @internal
*/
type _ExtractActionsFromSetupStore<SS> = _SpreadPropertiesFromObject<
SS,
_UnionToTuple<keyof SS>,
_Method
>

/**
* @internal
*/
type _ExtractGettersFromSetupStore<SS> = _SpreadPropertiesFromObject<
SS,
_UnionToTuple<keyof SS>,
ComputedRef<any>
>

/**
* Extract the actions of a store type. Works with both a Setup Store or an
* Options Store.
Expand Down
61 changes: 59 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DebuggerEvent, Ref, UnwrapRef } from 'vue'
import { ComputedRef, DebuggerEvent, Ref, UnwrapRef } from 'vue'
import { Pinia } from './rootStore'

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

/**
* @internal
*/
type _SpreadStateFromStore<SS, K extends readonly any[]> = K extends readonly [
infer A,
...infer Rest
]
? A extends string | number | symbol
? SS extends Record<A, _Method | ComputedRef<any>>
? _SpreadStateFromStore<SS, Rest>
: SS extends Record<A, any>
? Record<A, UnwrapRef<SS[A]>> & _SpreadStateFromStore<SS, Rest>
: never
: {}
: {}

/**
* @internal
*/
type _SpreadPropertiesFromObject<
SS,
K extends readonly any[],
T
> = K extends readonly [infer A, ...infer Rest]
? A extends string | number | symbol
? SS extends Record<A, T>
? Record<A, UnwrapRef<SS[A]>> & _SpreadPropertiesFromObject<SS, Rest, T>
: _SpreadPropertiesFromObject<SS, Rest, T>
: {}
: {}

/**
* @internal
*/
export type _ExtractStateFromSetupStore<SS> = _SpreadStateFromStore<
SS,
_UnionToTuple<keyof SS>
>

/**
* @internal
*/
export type _ExtractActionsFromSetupStore<SS> = _SpreadPropertiesFromObject<
SS,
_UnionToTuple<keyof SS>,
_Method
>

/**
* @internal
*/
export type _ExtractGettersFromSetupStore<SS> = _SpreadPropertiesFromObject<
SS,
_UnionToTuple<keyof SS>,
ComputedRef<any>
>

/**
* Options parameter of `defineStore()` for option stores. Can be extended to
* augment stores with the plugin API. @see {@link DefineStoreOptionsBase}.
Expand Down Expand Up @@ -595,7 +652,7 @@ export interface DefineStoreOptions<
*/
export interface DefineSetupStoreOptions<
Id extends string,
// TODO: pass SS instead
// NOTE: Passing SS seems to make TS crash
S extends StateTree,
G,
A /* extends ActionsTree */
Expand Down

0 comments on commit d71d89f

Please sign in to comment.