export type GettersReturn = { [K in keyof G]: G[K] extends (state: any) => infer R ? R : never; }; export type QueriesReturn = { [K in keyof Q]: Q[K] extends (state: any) => Query ? QueryState : never; }; /** * The full store state (used inside getters, queries, and actions) * consists of: * - the raw state, * - computed getters, * - reactive query states. */ export type StoreStateType = T & GettersReturn & QueriesReturn; /** * The two “special” methods available on the store. * (The keys here are derived from the union of getter and query keys.) */ export type SpecialActions = { $underive(keys: (keyof (GettersReturn & QueriesReturn))[]): void; $invalidate(keys: (keyof (GettersReturn & QueriesReturn))[]): void; }; /** * Actions get `this` as the complete store (state, getters, queries) * plus the two special methods. */ export type Actions = { [K: string]: (this: StoreStateType & SpecialActions, ...args: any[]) => any; }; /** * The final store type. * In addition to raw state, getters and query states, * each action is “unwrapped” so that it appears as a normal method. */ export type Store> = StoreStateType & { [K in keyof A]: A[K] extends (this: any, ...args: infer P) => infer R ? (...args: P) => R : never; } & SpecialActions; export type QueryFunction = () => Promise; export type QueryDefinition = { fn: QueryFunction; }; export type Query = QueryDefinition; export type QueryState = { value: T | undefined; isLoading: boolean; isFetching: boolean; error: Error | null; }; /** * The store definition accepts: * - a raw state creator, * - a set of getters (each receiving the full store state), * - a set of queries (each receiving the full store state and returning a Query), * - a set of actions. * * The generics enforce that: * - `G` extends a map of functions taking a StoreStateType, * - `Q` extends a map of functions taking a StoreStateType and returning a Query, * - `A` extends Actions using those types. */ export type StoreDefinition) => any> = {}, Q extends Record) => Query> = {}, A extends Actions = {}> = { state?(): T; getters?: G; queries?: Q; actions?: A; }; export declare function defineStore) => any>, Q extends Record) => Query>, A extends Actions>(definition: StoreDefinition): Store;