From: Eduardo San Martin Morote Date: Mon, 3 May 2021 10:43:56 +0000 (+0200) Subject: refactor(types): expose types as needed X-Git-Tag: v0.4.0~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c70932a88dc25c8872b261a35cc813710aff0300;p=thirdparty%2Fvuejs%2Fpinia.git refactor(types): expose types as needed --- diff --git a/src/index.ts b/src/index.ts index 1bbc646b..f121a2cc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,6 +13,8 @@ export { StoreDefinition, GenericStore, StoreWithGetters, + GettersTree, + _Method, StoreWithActions, StoreWithState, PiniaCustomProperties, @@ -27,6 +29,13 @@ export { mapGetters, MapStoresCustomization, setMapStoreSuffix, + _MapActionsObjectReturn, + _MapActionsReturn, + _MapStateObjectReturn, + _MapStateReturn, + _MapWritableStateObjectReturn, + _MapWritableStateReturn, + _Spread, } from './mapHelpers' // TODO: remove in beta diff --git a/src/mapHelpers.ts b/src/mapHelpers.ts index 72b8f27e..8726f7fa 100644 --- a/src/mapHelpers.ts +++ b/src/mapHelpers.ts @@ -2,7 +2,7 @@ import type Vue from 'vue' import { GenericStore, GettersTree, - Method, + _Method, StateTree, Store, StoreDefinition, @@ -44,15 +44,18 @@ type StoreObject = S extends StoreDefinition< } : {} -type Spread = A extends [infer L, ...infer R] - ? StoreObject & Spread +/** + * @internal + */ +export type _Spread = A extends [infer L, ...infer R] + ? StoreObject & _Spread : unknown function getCachedStore< Id extends string = string, S extends StateTree = StateTree, G extends GettersTree = GettersTree, - A = Record + A = Record >( vm: ComponentPublicInstance, useStore: StoreDefinition @@ -103,7 +106,7 @@ export function setMapStoreSuffix( */ export function mapStores( ...stores: [...Stores] -): Spread { +): _Spread { if (__DEV__ && Array.isArray(stores[0])) { console.warn( `[🍍]: Directly pass all stores to "mapStores()" without putting them in an array:\n` + @@ -122,14 +125,20 @@ export function mapStores( return getCachedStore(this, useStore) } return reduced - }, {} as Spread) + }, {} as _Spread) } -type MapStateReturn> = { +/** + * @internal + */ +export type _MapStateReturn> = { [key in keyof S | keyof G]: () => Store[key] } -type MapStateObjectReturn< +/** + * @internal + */ +export type _MapStateObjectReturn< Id extends string, S extends StateTree, G extends GettersTree, @@ -194,7 +203,7 @@ export function mapState< >( useStore: StoreDefinition, keyMapper: KeyMapper -): MapStateObjectReturn +): _MapStateObjectReturn /** * Allows using state and getters from one store without using the composition * API (`setup()`) by generating an object to be spread in the `computed` field @@ -226,7 +235,7 @@ export function mapState< >( useStore: StoreDefinition, keys: Array -): MapStateReturn +): _MapStateReturn /** * Allows using state and getters from one store without using the composition * API (`setup()`) by generating an object to be spread in the `computed` field @@ -247,14 +256,14 @@ export function mapState< >( useStore: StoreDefinition, keysOrMapper: Array | KeyMapper -): MapStateReturn | MapStateObjectReturn { +): _MapStateReturn | _MapStateObjectReturn { return Array.isArray(keysOrMapper) ? keysOrMapper.reduce((reduced, key) => { reduced[key] = function (this: ComponentPublicInstance) { return getCachedStore(this, useStore)[key] } as () => any return reduced - }, {} as MapStateReturn) + }, {} as _MapStateReturn) : Object.keys(keysOrMapper).reduce((reduced, key: keyof KeyMapper) => { reduced[key] = function (this: ComponentPublicInstance) { const store = getCachedStore(this, useStore) @@ -266,7 +275,7 @@ export function mapState< : store[storeKey as keyof S | keyof G] } return reduced - }, {} as MapStateObjectReturn) + }, {} as _MapStateObjectReturn) } /** @@ -275,11 +284,17 @@ export function mapState< */ export const mapGetters = mapState -type MapActionsReturn = { +/** + * @internal + */ +export type _MapActionsReturn = { [key in keyof A]: Store[key] } -type MapActionsObjectReturn> = { +/** + * @internal + */ +export type _MapActionsObjectReturn> = { [key in keyof T]: Store[T[key]] } @@ -317,7 +332,7 @@ export function mapActions< >( useStore: StoreDefinition, keyMapper: KeyMapper -): MapActionsObjectReturn +): _MapActionsObjectReturn /** * Allows directly using actions from your store without using the composition * API (`setup()`) by generating an object to be spread in the `methods` field @@ -349,7 +364,7 @@ export function mapActions< >( useStore: StoreDefinition, keys: Array -): MapActionsReturn +): _MapActionsReturn /** * Allows directly using actions from your store without using the composition * API (`setup()`) by generating an object to be spread in the `methods` field @@ -367,17 +382,17 @@ export function mapActions< >( useStore: StoreDefinition, keysOrMapper: Array | KeyMapper -): MapActionsReturn | MapActionsObjectReturn { +): _MapActionsReturn | _MapActionsObjectReturn { return Array.isArray(keysOrMapper) ? keysOrMapper.reduce((reduced, key) => { reduced[key] = function ( this: ComponentPublicInstance, ...args: any[] ) { - return (getCachedStore(this, useStore)[key] as Method)(...args) + return (getCachedStore(this, useStore)[key] as _Method)(...args) } as Store[keyof A] return reduced - }, {} as MapActionsReturn) + }, {} as _MapActionsReturn) : Object.keys(keysOrMapper).reduce((reduced, key: keyof KeyMapper) => { reduced[key] = function ( this: ComponentPublicInstance, @@ -386,17 +401,23 @@ export function mapActions< return getCachedStore(this, useStore)[keysOrMapper[key]](...args) } as Store[keyof KeyMapper[]] return reduced - }, {} as MapActionsObjectReturn) + }, {} as _MapActionsObjectReturn) } -type MapWritableStateReturn = { +/** + * @internal + */ +export type _MapWritableStateReturn = { [key in keyof S]: { get: () => Store[key] set: (value: Store[key]) => any } } -type MapWritableStateObjectReturn< +/** + * @internal + */ +export type _MapWritableStateObjectReturn< S extends StateTree, T extends Record > = { @@ -423,7 +444,7 @@ export function mapWritableState< >( useStore: StoreDefinition, keyMapper: KeyMapper -): MapWritableStateObjectReturn +): _MapWritableStateObjectReturn /** * Allows using state and getters from one store without using the composition * API (`setup()`) by generating an object to be spread in the `computed` field @@ -440,7 +461,7 @@ export function mapWritableState< >( useStore: StoreDefinition, keys: Array -): MapWritableStateReturn +): _MapWritableStateReturn /** * Allows using state and getters from one store without using the composition * API (`setup()`) by generating an object to be spread in the `computed` field @@ -458,7 +479,7 @@ export function mapWritableState< >( useStore: StoreDefinition, keysOrMapper: Array | KeyMapper -): MapWritableStateReturn | MapWritableStateObjectReturn { +): _MapWritableStateReturn | _MapWritableStateObjectReturn { return Array.isArray(keysOrMapper) ? keysOrMapper.reduce((reduced, key) => { // @ts-ignore @@ -472,7 +493,7 @@ export function mapWritableState< }, } return reduced - }, {} as MapWritableStateReturn) + }, {} as _MapWritableStateReturn) : Object.keys(keysOrMapper).reduce((reduced, key: keyof KeyMapper) => { // @ts-ignore reduced[key] = { @@ -487,5 +508,5 @@ export function mapWritableState< }, } return reduced - }, {} as MapWritableStateObjectReturn) + }, {} as _MapWritableStateObjectReturn) } diff --git a/src/rootStore.ts b/src/rootStore.ts index 11127d88..4590074d 100644 --- a/src/rootStore.ts +++ b/src/rootStore.ts @@ -6,7 +6,7 @@ import { PiniaCustomProperties, GenericStore, GettersTree, - Method, + _Method, Store, DefineStoreOptions, } from './types' @@ -43,7 +43,7 @@ export interface PiniaPluginContext< Id extends string = string, S extends StateTree = StateTree, G extends GettersTree = GettersTree, - A = Record + A = Record > { /** * pinia instance. @@ -70,6 +70,12 @@ export interface PiniaPluginContext< * Plugin to extend every store */ export interface PiniaStorePlugin { + /** + * Plugin to extend every store. Returns an object to extend the store or + * nothing. + * + * @param context - Context + */ (context: PiniaPluginContext): Partial | void } diff --git a/src/store.ts b/src/store.ts index d72f91ce..b73a6a01 100644 --- a/src/store.ts +++ b/src/store.ts @@ -19,7 +19,7 @@ import { StoreWithGetters, Store, StoreWithActions, - Method, + _Method, StateDescriptor, StoreDefinition, GettersTree, @@ -215,7 +215,7 @@ function buildStoreToUse< Id extends string, S extends StateTree, G extends GettersTree, - A extends Record + A extends Record >( partialStore: StoreWithState, descriptor: StateDescriptor, @@ -321,7 +321,7 @@ export function defineStore< storeAndDescriptor[1], id, getters as GettersTree | undefined, - actions as Record | undefined, + actions as Record | undefined, // @ts-expect-error: because of the extend on Actions options ) @@ -342,7 +342,7 @@ export function defineStore< storeAndDescriptor[1], id, getters as GettersTree | undefined, - actions as Record | undefined, + actions as Record | undefined, // @ts-expect-error: because of the extend on Actions options ) diff --git a/src/types.ts b/src/types.ts index d0d9fa39..ca1b1dd0 100644 --- a/src/types.ts +++ b/src/types.ts @@ -87,7 +87,12 @@ export interface StoreWithState { $subscribe(callback: SubscriptionCallback): () => void } -export type Method = (...args: any[]) => any +/** + * Generic type for a function that can infer arguments and return type + * + * @internal + */ +export type _Method = (...args: any[]) => any // export type StoreAction

= (...args: P) => R // export interface StoreAction { @@ -149,7 +154,16 @@ export interface StoreDefinition< G extends GettersTree, A /* extends Record */ > { + /** + * Returns a store, creates it if necessary. + * + * @param pinia - Pinia instance to retrieve the store + */ (pinia?: Pinia | null | undefined): Store + + /** + * Id of the store. Used by map helpers. + */ $id: Id } @@ -160,7 +174,7 @@ export type GenericStore = Store< string, StateTree, GettersTree, - Record + Record > /** @@ -170,7 +184,7 @@ export type GenericStoreDefinition = StoreDefinition< string, StateTree, GettersTree, - Record + Record > /** @@ -180,9 +194,14 @@ export interface PiniaCustomProperties< Id extends string = string, S extends StateTree = StateTree, G extends GettersTree = GettersTree, - A = Record + A = Record > {} +/** + * Type of an object of Getters that infers the argument + * + * @internal + */ export type GettersTree = Record< string, ((state: S) => any) | (() => any) @@ -198,10 +217,21 @@ export interface DefineStoreOptions< G extends GettersTree, A /* extends Record */ > { + /** + * Unique string key to identify the store across the application. + */ id: Id + /** + * Function to create a fresh state. + */ state?: () => S + /** + * Optional object of getters. + */ getters?: G & ThisType & PiniaCustomProperties> - // allow actions use other actions + /** + * Optional object of actions. + */ actions?: A & ThisType< A &