pinia.d.ts 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. import { App } from 'vue-demi';
  2. import { ComputedRef } from 'vue-demi';
  3. import type { DebuggerEvent } from 'vue-demi';
  4. import { EffectScope } from 'vue-demi';
  5. import type { Plugin as Plugin_2 } from 'vue-demi';
  6. import { Ref } from 'vue-demi';
  7. import { ToRef } from 'vue-demi';
  8. import { ToRefs } from 'vue-demi';
  9. import { UnwrapRef } from 'vue-demi';
  10. import type { WatchOptions } from 'vue-demi';
  11. /**
  12. * Creates an _accept_ function to pass to `import.meta.hot` in Vite applications.
  13. *
  14. * @example
  15. * ```js
  16. * const useUser = defineStore(...)
  17. * if (import.meta.hot) {
  18. * import.meta.hot.accept(acceptHMRUpdate(useUser, import.meta.hot))
  19. * }
  20. * ```
  21. *
  22. * @param initialUseStore - return of the defineStore to hot update
  23. * @param hot - `import.meta.hot`
  24. */
  25. export declare function acceptHMRUpdate<Id extends string = string, S extends StateTree = StateTree, G extends _GettersTree<S> = _GettersTree<S>, A = _ActionsTree>(initialUseStore: StoreDefinition<Id, S, G, A>, hot: any): (newModule: any) => any;
  26. /**
  27. * Type of an object of Actions. For internal usage only.
  28. * For internal use **only**
  29. */
  30. export declare type _ActionsTree = Record<string, _Method>;
  31. export declare type _Awaited<T> = T extends null | undefined ? T : T extends object & {
  32. then(onfulfilled: infer F): any;
  33. } ? F extends (value: infer V, ...args: any) => any ? _Awaited<V> : never : T;
  34. /**
  35. * Creates a Pinia instance to be used by the application
  36. */
  37. export declare function createPinia(): Pinia;
  38. /**
  39. * Recursive `Partial<T>`. Used by {@link Store['$patch']}.
  40. *
  41. * For internal use **only**
  42. */
  43. export declare type _DeepPartial<T> = {
  44. [K in keyof T]?: _DeepPartial<T[K]>;
  45. };
  46. /**
  47. * Options parameter of `defineStore()` for setup stores. Can be extended to
  48. * augment stores with the plugin API. @see {@link DefineStoreOptionsBase}.
  49. */
  50. export declare interface DefineSetupStoreOptions<Id extends string, S extends StateTree, G, A> extends DefineStoreOptionsBase<S, Store<Id, S, G, A>> {
  51. /**
  52. * Extracted actions. Added by useStore(). SHOULD NOT be added by the user when
  53. * creating the store. Can be used in plugins to get the list of actions in a
  54. * store defined with a setup function. Note this is always defined
  55. */
  56. actions?: A;
  57. }
  58. /**
  59. * Creates a `useStore` function that retrieves the store instance
  60. *
  61. * @param id - id of the store (must be unique)
  62. * @param options - options to define the store
  63. */
  64. export declare function defineStore<Id extends string, S extends StateTree = {}, G extends _GettersTree<S> = {}, A = {}>(id: Id, options: Omit<DefineStoreOptions<Id, S, G, A>, 'id'>): StoreDefinition<Id, S, G, A>;
  65. /**
  66. * Creates a `useStore` function that retrieves the store instance
  67. *
  68. * @param options - options to define the store
  69. */
  70. export declare function defineStore<Id extends string, S extends StateTree = {}, G extends _GettersTree<S> = {}, A = {}>(options: DefineStoreOptions<Id, S, G, A>): StoreDefinition<Id, S, G, A>;
  71. /**
  72. * Creates a `useStore` function that retrieves the store instance
  73. *
  74. * @param id - id of the store (must be unique)
  75. * @param storeSetup - function that defines the store
  76. * @param options - extra options
  77. */
  78. export declare function defineStore<Id extends string, SS>(id: Id, storeSetup: () => SS, options?: DefineSetupStoreOptions<Id, _ExtractStateFromSetupStore<SS>, _ExtractGettersFromSetupStore<SS>, _ExtractActionsFromSetupStore<SS>>): StoreDefinition<Id, _ExtractStateFromSetupStore<SS>, _ExtractGettersFromSetupStore<SS>, _ExtractActionsFromSetupStore<SS>>;
  79. /**
  80. * Options parameter of `defineStore()` for option stores. Can be extended to
  81. * augment stores with the plugin API. @see {@link DefineStoreOptionsBase}.
  82. */
  83. export declare interface DefineStoreOptions<Id extends string, S extends StateTree, G, A> extends DefineStoreOptionsBase<S, Store<Id, S, G, A>> {
  84. /**
  85. * Unique string key to identify the store across the application.
  86. */
  87. id: Id;
  88. /**
  89. * Function to create a fresh state. **Must be an arrow function** to ensure
  90. * correct typings!
  91. */
  92. state?: () => S;
  93. /**
  94. * Optional object of getters.
  95. */
  96. getters?: G & ThisType<UnwrapRef<S> & _StoreWithGetters<G> & PiniaCustomProperties> & _GettersTree<S>;
  97. /**
  98. * Optional object of actions.
  99. */
  100. actions?: A & ThisType<A & UnwrapRef<S> & _StoreWithState<Id, S, G, A> & _StoreWithGetters<G> & PiniaCustomProperties>;
  101. /**
  102. * Allows hydrating the store during SSR when complex state (like client side only refs) are used in the store
  103. * definition and copying the value from `pinia.state` isn't enough.
  104. *
  105. * @example
  106. * If in your `state`, you use any `customRef`s, any `computed`s, or any `ref`s that have a different value on
  107. * Server and Client, you need to manually hydrate them. e.g., a custom ref that is stored in the local
  108. * storage:
  109. *
  110. * ```ts
  111. * const useStore = defineStore('main', {
  112. * state: () => ({
  113. * n: useLocalStorage('key', 0)
  114. * }),
  115. * hydrate(storeState, initialState) {
  116. * // @ts-expect-error: https://github.com/microsoft/TypeScript/issues/43826
  117. * storeState.n = useLocalStorage('key', 0)
  118. * }
  119. * })
  120. * ```
  121. *
  122. * @param storeState - the current state in the store
  123. * @param initialState - initialState
  124. */
  125. hydrate?(storeState: UnwrapRef<S>, initialState: UnwrapRef<S>): void;
  126. }
  127. /**
  128. * Options passed to `defineStore()` that are common between option and setup
  129. * stores. Extend this interface if you want to add custom options to both kinds
  130. * of stores.
  131. */
  132. export declare interface DefineStoreOptionsBase<S extends StateTree, Store> {
  133. }
  134. /**
  135. * Available `options` when creating a pinia plugin.
  136. */
  137. export declare interface DefineStoreOptionsInPlugin<Id extends string, S extends StateTree, G, A> extends Omit<DefineStoreOptions<Id, S, G, A>, 'id' | 'actions'> {
  138. /**
  139. * Extracted object of actions. Added by useStore() when the store is built
  140. * using the setup API, otherwise uses the one passed to `defineStore()`.
  141. * Defaults to an empty object if no actions are defined.
  142. */
  143. actions: A;
  144. }
  145. /**
  146. * For internal use **only**
  147. */
  148. export declare type _ExtractActionsFromSetupStore<SS> = SS extends undefined | void ? {} : _ExtractActionsFromSetupStore_Keys<SS> extends keyof SS ? Pick<SS, _ExtractActionsFromSetupStore_Keys<SS>> : never;
  149. /**
  150. * Type that enables refactoring through IDE.
  151. * For internal use **only**
  152. */
  153. export declare type _ExtractActionsFromSetupStore_Keys<SS> = keyof {
  154. [K in keyof SS as SS[K] extends _Method ? K : never]: any;
  155. };
  156. /**
  157. * For internal use **only**
  158. */
  159. export declare type _ExtractGettersFromSetupStore<SS> = SS extends undefined | void ? {} : _ExtractGettersFromSetupStore_Keys<SS> extends keyof SS ? Pick<SS, _ExtractGettersFromSetupStore_Keys<SS>> : never;
  160. /**
  161. * Type that enables refactoring through IDE.
  162. * For internal use **only**
  163. */
  164. export declare type _ExtractGettersFromSetupStore_Keys<SS> = keyof {
  165. [K in keyof SS as SS[K] extends ComputedRef ? K : never]: any;
  166. };
  167. /**
  168. * For internal use **only**
  169. */
  170. export declare type _ExtractStateFromSetupStore<SS> = SS extends undefined | void ? {} : _ExtractStateFromSetupStore_Keys<SS> extends keyof SS ? _UnwrapAll<Pick<SS, _ExtractStateFromSetupStore_Keys<SS>>> : never;
  171. /**
  172. * Type that enables refactoring through IDE.
  173. * For internal use **only**
  174. */
  175. export declare type _ExtractStateFromSetupStore_Keys<SS> = keyof {
  176. [K in keyof SS as SS[K] extends _Method | ComputedRef ? never : K]: any;
  177. };
  178. /**
  179. * Get the currently active pinia if there is any.
  180. */
  181. export declare const getActivePinia: () => Pinia | undefined;
  182. /**
  183. * Type of an object of Getters that infers the argument. For internal usage only.
  184. * For internal use **only**
  185. */
  186. export declare type _GettersTree<S extends StateTree> = Record<string, ((state: UnwrapRef<S> & UnwrapRef<PiniaCustomStateProperties<S>>) => any) | (() => any)>;
  187. /**
  188. * Allows directly using actions from your store without using the composition
  189. * API (`setup()`) by generating an object to be spread in the `methods` field
  190. * of a component. The values of the object are the actions while the keys are
  191. * the names of the resulting methods.
  192. *
  193. * @example
  194. * ```js
  195. * export default {
  196. * methods: {
  197. * // other methods properties
  198. * // useCounterStore has two actions named `increment` and `setCount`
  199. * ...mapActions(useCounterStore, { moar: 'increment', setIt: 'setCount' })
  200. * },
  201. *
  202. * created() {
  203. * this.moar()
  204. * this.setIt(2)
  205. * }
  206. * }
  207. * ```
  208. *
  209. * @param useStore - store to map from
  210. * @param keyMapper - object to define new names for the actions
  211. */
  212. export declare function mapActions<Id extends string, S extends StateTree, G extends _GettersTree<S>, A, KeyMapper extends Record<string, keyof A>>(useStore: StoreDefinition<Id, S, G, A>, keyMapper: KeyMapper): _MapActionsObjectReturn<A, KeyMapper>;
  213. /**
  214. * Allows directly using actions from your store without using the composition
  215. * API (`setup()`) by generating an object to be spread in the `methods` field
  216. * of a component.
  217. *
  218. * @example
  219. * ```js
  220. * export default {
  221. * methods: {
  222. * // other methods properties
  223. * ...mapActions(useCounterStore, ['increment', 'setCount'])
  224. * },
  225. *
  226. * created() {
  227. * this.increment()
  228. * this.setCount(2) // pass arguments as usual
  229. * }
  230. * }
  231. * ```
  232. *
  233. * @param useStore - store to map from
  234. * @param keys - array of action names to map
  235. */
  236. export declare function mapActions<Id extends string, S extends StateTree, G extends _GettersTree<S>, A>(useStore: StoreDefinition<Id, S, G, A>, keys: Array<keyof A>): _MapActionsReturn<A>;
  237. /**
  238. * For internal use **only**
  239. */
  240. export declare type _MapActionsObjectReturn<A, T extends Record<string, keyof A>> = {
  241. [key in keyof T]: A[T[key]];
  242. };
  243. /**
  244. * For internal use **only**
  245. */
  246. export declare type _MapActionsReturn<A> = {
  247. [key in keyof A]: A[key];
  248. };
  249. /**
  250. * Alias for `mapState()`. You should use `mapState()` instead.
  251. * @deprecated use `mapState()` instead.
  252. */
  253. export declare const mapGetters: typeof mapState;
  254. /**
  255. * Allows using state and getters from one store without using the composition
  256. * API (`setup()`) by generating an object to be spread in the `computed` field
  257. * of a component. The values of the object are the state properties/getters
  258. * while the keys are the names of the resulting computed properties.
  259. * Optionally, you can also pass a custom function that will receive the store
  260. * as its first argument. Note that while it has access to the component
  261. * instance via `this`, it won't be typed.
  262. *
  263. * @example
  264. * ```js
  265. * export default {
  266. * computed: {
  267. * // other computed properties
  268. * // useCounterStore has a state property named `count` and a getter `double`
  269. * ...mapState(useCounterStore, {
  270. * n: 'count',
  271. * triple: store => store.n * 3,
  272. * // note we can't use an arrow function if we want to use `this`
  273. * custom(store) {
  274. * return this.someComponentValue + store.n
  275. * },
  276. * doubleN: 'double'
  277. * })
  278. * },
  279. *
  280. * created() {
  281. * this.n // 2
  282. * this.doubleN // 4
  283. * }
  284. * }
  285. * ```
  286. *
  287. * @param useStore - store to map from
  288. * @param keyMapper - object of state properties or getters
  289. */
  290. export declare function mapState<Id extends string, S extends StateTree, G extends _GettersTree<S>, A, KeyMapper extends Record<string, keyof S | keyof G | ((store: Store<Id, S, G, A>) => any)>>(useStore: StoreDefinition<Id, S, G, A>, keyMapper: KeyMapper): _MapStateObjectReturn<Id, S, G, A, KeyMapper>;
  291. /**
  292. * Allows using state and getters from one store without using the composition
  293. * API (`setup()`) by generating an object to be spread in the `computed` field
  294. * of a component.
  295. *
  296. * @example
  297. * ```js
  298. * export default {
  299. * computed: {
  300. * // other computed properties
  301. * ...mapState(useCounterStore, ['count', 'double'])
  302. * },
  303. *
  304. * created() {
  305. * this.count // 2
  306. * this.double // 4
  307. * }
  308. * }
  309. * ```
  310. *
  311. * @param useStore - store to map from
  312. * @param keys - array of state properties or getters
  313. */
  314. export declare function mapState<Id extends string, S extends StateTree, G extends _GettersTree<S>, A, Keys extends keyof S | keyof G>(useStore: StoreDefinition<Id, S, G, A>, keys: readonly Keys[]): _MapStateReturn<S, G, Keys>;
  315. /**
  316. * For internal use **only**
  317. */
  318. export declare type _MapStateObjectReturn<Id extends string, S extends StateTree, G extends _GettersTree<S>, A, T extends Record<string, keyof S | keyof G | ((store: Store<Id, S, G, A>) => any)> = {}> = {
  319. [key in keyof T]: () => T[key] extends (store: any) => infer R ? R : T[key] extends keyof Store<Id, S, G, A> ? Store<Id, S, G, A>[T[key]] : never;
  320. };
  321. /**
  322. * For internal use **only**
  323. */
  324. export declare type _MapStateReturn<S extends StateTree, G extends _GettersTree<S>, Keys extends keyof S | keyof G = keyof S | keyof G> = {
  325. [key in Keys]: () => Store<string, S, G, {}>[key];
  326. };
  327. /**
  328. * Allows using stores without the composition API (`setup()`) by generating an
  329. * object to be spread in the `computed` field of a component. It accepts a list
  330. * of store definitions.
  331. *
  332. * @example
  333. * ```js
  334. * export default {
  335. * computed: {
  336. * // other computed properties
  337. * ...mapStores(useUserStore, useCartStore)
  338. * },
  339. *
  340. * created() {
  341. * this.userStore // store with id "user"
  342. * this.cartStore // store with id "cart"
  343. * }
  344. * }
  345. * ```
  346. *
  347. * @param stores - list of stores to map to an object
  348. */
  349. export declare function mapStores<Stores extends any[]>(...stores: [...Stores]): _Spread<Stores>;
  350. /**
  351. * Interface to allow customizing map helpers. Extend this interface with the
  352. * following properties:
  353. *
  354. * - `suffix`: string. Affects the suffix of `mapStores()`, defaults to `Store`.
  355. */
  356. export declare interface MapStoresCustomization {
  357. }
  358. /**
  359. * Same as `mapState()` but creates computed setters as well so the state can be
  360. * modified. Differently from `mapState()`, only `state` properties can be
  361. * added.
  362. *
  363. * @param useStore - store to map from
  364. * @param keyMapper - object of state properties
  365. */
  366. export declare function mapWritableState<Id extends string, S extends StateTree, G extends _GettersTree<S>, A, KeyMapper extends Record<string, keyof S>>(useStore: StoreDefinition<Id, S, G, A>, keyMapper: KeyMapper): _MapWritableStateObjectReturn<S, KeyMapper>;
  367. /**
  368. * Allows using state and getters from one store without using the composition
  369. * API (`setup()`) by generating an object to be spread in the `computed` field
  370. * of a component.
  371. *
  372. * @param useStore - store to map from
  373. * @param keys - array of state properties
  374. */
  375. export declare function mapWritableState<Id extends string, S extends StateTree, G extends _GettersTree<S>, A, Keys extends keyof S>(useStore: StoreDefinition<Id, S, G, A>, keys: readonly Keys[]): {
  376. [K in Keys]: {
  377. get: () => S[K];
  378. set: (value: S[K]) => any;
  379. };
  380. };
  381. /**
  382. * For internal use **only**
  383. */
  384. export declare type _MapWritableStateObjectReturn<S extends StateTree, T extends Record<string, keyof S>> = {
  385. [key in keyof T]: {
  386. get: () => S[T[key]];
  387. set: (value: S[T[key]]) => any;
  388. };
  389. };
  390. /**
  391. * For internal use **only**
  392. */
  393. export declare type _MapWritableStateReturn<S extends StateTree> = {
  394. [key in keyof S]: {
  395. get: () => S[key];
  396. set: (value: S[key]) => any;
  397. };
  398. };
  399. /**
  400. * Generic type for a function that can infer arguments and return type
  401. *
  402. * For internal use **only**
  403. */
  404. export declare type _Method = (...args: any[]) => any;
  405. /**
  406. * Possible types for SubscriptionCallback
  407. */
  408. export declare enum MutationType {
  409. /**
  410. * Direct mutation of the state:
  411. *
  412. * - `store.name = 'new name'`
  413. * - `store.$state.name = 'new name'`
  414. * - `store.list.push('new item')`
  415. */
  416. direct = "direct",
  417. /**
  418. * Mutated the state with `$patch` and an object
  419. *
  420. * - `store.$patch({ name: 'newName' })`
  421. */
  422. patchObject = "patch object",
  423. /**
  424. * Mutated the state with `$patch` and a function
  425. *
  426. * - `store.$patch(state => state.name = 'newName')`
  427. */
  428. patchFunction = "patch function"
  429. }
  430. /**
  431. * Every application must own its own pinia to be able to create stores
  432. */
  433. export declare interface Pinia {
  434. install: (app: App) => void;
  435. /**
  436. * root state
  437. */
  438. state: Ref<Record<string, StateTree>>;
  439. /**
  440. * Adds a store plugin to extend every store
  441. *
  442. * @param plugin - store plugin to add
  443. */
  444. use(plugin: PiniaPlugin): Pinia;
  445. /* Excluded from this release type: _p */
  446. /* Excluded from this release type: _a */
  447. /* Excluded from this release type: _e */
  448. /* Excluded from this release type: _s */
  449. /* Excluded from this release type: _testing */
  450. }
  451. /**
  452. * Interface to be extended by the user when they add properties through plugins.
  453. */
  454. export declare interface PiniaCustomProperties<Id extends string = string, S extends StateTree = StateTree, G = _GettersTree<S>, A = _ActionsTree> {
  455. }
  456. /**
  457. * Properties that are added to every `store.$state` by `pinia.use()`.
  458. */
  459. export declare interface PiniaCustomStateProperties<S extends StateTree = StateTree> {
  460. }
  461. /**
  462. * Plugin to extend every store.
  463. */
  464. export declare interface PiniaPlugin {
  465. /**
  466. * Plugin to extend every store. Returns an object to extend the store or
  467. * nothing.
  468. *
  469. * @param context - Context
  470. */
  471. (context: PiniaPluginContext): Partial<PiniaCustomProperties & PiniaCustomStateProperties> | void;
  472. }
  473. /**
  474. * Context argument passed to Pinia plugins.
  475. */
  476. export declare interface PiniaPluginContext<Id extends string = string, S extends StateTree = StateTree, G = _GettersTree<S>, A = _ActionsTree> {
  477. /**
  478. * pinia instance.
  479. */
  480. pinia: Pinia;
  481. /**
  482. * Current app created with `Vue.createApp()`.
  483. */
  484. app: App;
  485. /**
  486. * Current store being extended.
  487. */
  488. store: Store<Id, S, G, A>;
  489. /**
  490. * Initial options defining the store when calling `defineStore()`.
  491. */
  492. options: DefineStoreOptionsInPlugin<Id, S, G, A>;
  493. }
  494. /**
  495. * Plugin to extend every store.
  496. * @deprecated use PiniaPlugin instead
  497. */
  498. export declare type PiniaStorePlugin = PiniaPlugin;
  499. /**
  500. * Vue 2 Plugin that must be installed for pinia to work. Note **you don't need
  501. * this plugin if you are using Nuxt.js**. Use the `buildModule` instead:
  502. * https://pinia.vuejs.org/ssr/nuxt.html.
  503. *
  504. * @example
  505. * ```js
  506. * import Vue from 'vue'
  507. * import { PiniaVuePlugin, createPinia } from 'pinia'
  508. *
  509. * Vue.use(PiniaVuePlugin)
  510. * const pinia = createPinia()
  511. *
  512. * new Vue({
  513. * el: '#app',
  514. * // ...
  515. * pinia,
  516. * })
  517. * ```
  518. *
  519. * @param _Vue - `Vue` imported from 'vue'.
  520. */
  521. export declare const PiniaVuePlugin: Plugin_2;
  522. declare interface _SetActivePinia {
  523. (pinia: Pinia): Pinia;
  524. (pinia: undefined): undefined;
  525. (pinia: Pinia | undefined): Pinia | undefined;
  526. }
  527. /**
  528. * Sets or unsets the active pinia. Used in SSR and internally when calling
  529. * actions and getters
  530. *
  531. * @param pinia - Pinia instance
  532. */
  533. export declare const setActivePinia: _SetActivePinia;
  534. /**
  535. * Changes the suffix added by `mapStores()`. Can be set to an empty string.
  536. * Defaults to `"Store"`. Make sure to extend the MapStoresCustomization
  537. * interface if you are using TypeScript.
  538. *
  539. * @param suffix - new suffix
  540. */
  541. export declare function setMapStoreSuffix(suffix: MapStoresCustomization extends Record<'suffix', infer Suffix> ? Suffix : string): void;
  542. /**
  543. * Tells Pinia to skip the hydration process of a given object. This is useful in setup stores (only) when you return a
  544. * stateful object in the store but it isn't really state. e.g. returning a router instance in a setup store.
  545. *
  546. * @param obj - target object
  547. * @returns obj
  548. */
  549. export declare function skipHydrate<T = any>(obj: T): T;
  550. /**
  551. * For internal use **only**.
  552. */
  553. export declare type _Spread<A extends readonly any[]> = A extends [infer L, ...infer R] ? _StoreObject<L> & _Spread<R> : unknown;
  554. /**
  555. * Generic state of a Store
  556. */
  557. export declare type StateTree = Record<string | number | symbol, any>;
  558. /**
  559. * Store type to build a store.
  560. */
  561. export declare type Store<Id extends string = string, S extends StateTree = {}, G = {}, A = {}> = _StoreWithState<Id, S, G, A> & UnwrapRef<S> & _StoreWithGetters<G> & (_ActionsTree extends A ? {} : A) & PiniaCustomProperties<Id, S, G, A> & PiniaCustomStateProperties<S>;
  562. /**
  563. * Extract the actions of a store type. Works with both a Setup Store or an
  564. * Options Store.
  565. */
  566. export declare type StoreActions<SS> = SS extends Store<string, StateTree, _GettersTree<StateTree>, infer A> ? A : _ExtractActionsFromSetupStore<SS>;
  567. /**
  568. * Return type of `defineStore()`. Function that allows instantiating a store.
  569. */
  570. export declare interface StoreDefinition<Id extends string = string, S extends StateTree = StateTree, G = _GettersTree<S>, A = _ActionsTree> {
  571. /**
  572. * Returns a store, creates it if necessary.
  573. *
  574. * @param pinia - Pinia instance to retrieve the store
  575. * @param hot - dev only hot module replacement
  576. */
  577. (pinia?: Pinia | null | undefined, hot?: StoreGeneric): Store<Id, S, G, A>;
  578. /**
  579. * Id of the store. Used by map helpers.
  580. */
  581. $id: Id;
  582. /* Excluded from this release type: _pinia */
  583. }
  584. /**
  585. * Generic and type-unsafe version of Store. Doesn't fail on access with
  586. * strings, making it much easier to write generic functions that do not care
  587. * about the kind of store that is passed.
  588. */
  589. export declare type StoreGeneric = Store<string, StateTree, _GettersTree<StateTree>, _ActionsTree>;
  590. /**
  591. * Extract the getters of a store type. Works with both a Setup Store or an
  592. * Options Store.
  593. */
  594. export declare type StoreGetters<SS> = SS extends Store<string, StateTree, infer G, _ActionsTree> ? _StoreWithGetters<G> : _ExtractGettersFromSetupStore<SS>;
  595. /**
  596. * For internal use **only**.
  597. */
  598. export declare type _StoreObject<S> = S extends StoreDefinition<infer Ids, infer State, infer Getters, infer Actions> ? {
  599. [Id in `${Ids}${MapStoresCustomization extends Record<'suffix', infer Suffix> ? Suffix : 'Store'}`]: () => Store<Id extends `${infer RealId}${MapStoresCustomization extends Record<'suffix', infer Suffix> ? Suffix : 'Store'}` ? RealId : string, State, Getters, Actions>;
  600. } : {};
  601. /**
  602. * Argument of `store.$onAction()`
  603. */
  604. export declare type StoreOnActionListener<Id extends string, S extends StateTree, G, A> = (context: StoreOnActionListenerContext<Id, S, G, {} extends A ? _ActionsTree : A>) => void;
  605. /**
  606. * Context object passed to callbacks of `store.$onAction(context => {})`
  607. * TODO: should have only the Id, the Store and Actions to generate the proper object
  608. */
  609. export declare type StoreOnActionListenerContext<Id extends string, S extends StateTree, G, A> = _ActionsTree extends A ? _StoreOnActionListenerContext<StoreGeneric, string, _ActionsTree> : {
  610. [Name in keyof A]: Name extends string ? _StoreOnActionListenerContext<Store<Id, S, G, A>, Name, A> : never;
  611. }[keyof A];
  612. /**
  613. * Actual type for {@link StoreOnActionListenerContext}. Exists for refactoring
  614. * purposes. For internal use only.
  615. * For internal use **only**
  616. */
  617. export declare interface _StoreOnActionListenerContext<Store, ActionName extends string, A> {
  618. /**
  619. * Name of the action
  620. */
  621. name: ActionName;
  622. /**
  623. * Store that is invoking the action
  624. */
  625. store: Store;
  626. /**
  627. * Parameters passed to the action
  628. */
  629. args: A extends Record<ActionName, _Method> ? Parameters<A[ActionName]> : unknown[];
  630. /**
  631. * Sets up a hook once the action is finished. It receives the return value
  632. * of the action, if it's a Promise, it will be unwrapped.
  633. */
  634. after: (callback: A extends Record<ActionName, _Method> ? (resolvedReturn: _Awaited<ReturnType<A[ActionName]>>) => void : () => void) => void;
  635. /**
  636. * Sets up a hook if the action fails. Return `false` to catch the error and
  637. * stop it from propagating.
  638. */
  639. onError: (callback: (error: unknown) => void) => void;
  640. }
  641. /**
  642. * Properties of a store.
  643. */
  644. export declare interface StoreProperties<Id extends string> {
  645. /**
  646. * Unique identifier of the store
  647. */
  648. $id: Id;
  649. /* Excluded from this release type: _p */
  650. /* Excluded from this release type: _getters */
  651. /* Excluded from this release type: _isOptionsAPI */
  652. /**
  653. * Used by devtools plugin to retrieve properties added with plugins. Removed
  654. * in production. Can be used by the user to add property keys of the store
  655. * that should be displayed in devtools.
  656. */
  657. _customProperties: Set<string>;
  658. /* Excluded from this release type: _hotUpdate */
  659. /* Excluded from this release type: _hotUpdating */
  660. /* Excluded from this release type: _hmrPayload */
  661. }
  662. /**
  663. * Extract the state of a store type. Works with both a Setup Store or an
  664. * Options Store. Note this unwraps refs.
  665. */
  666. export declare type StoreState<SS> = SS extends Store<string, infer S, _GettersTree<StateTree>, _ActionsTree> ? UnwrapRef<S> : _ExtractStateFromSetupStore<SS>;
  667. /**
  668. * Extracts the return type for `storeToRefs`.
  669. * Will convert any `getters` into `ComputedRef`.
  670. */
  671. declare type StoreToRefs<SS extends StoreGeneric> = ToRefs<StoreState<SS> & PiniaCustomStateProperties<StoreState<SS>>> & ToComputedRefs<StoreGetters<SS>>;
  672. /**
  673. * Creates an object of references with all the state, getters, and plugin-added
  674. * state properties of the store. Similar to `toRefs()` but specifically
  675. * designed for Pinia stores so methods and non reactive properties are
  676. * completely ignored.
  677. *
  678. * @param store - store to extract the refs from
  679. */
  680. export declare function storeToRefs<SS extends StoreGeneric>(store: SS): StoreToRefs<SS>;
  681. /**
  682. * Store augmented for actions. For internal usage only.
  683. * For internal use **only**
  684. */
  685. export declare type _StoreWithActions<A> = {
  686. [k in keyof A]: A[k] extends (...args: infer P) => infer R ? (...args: P) => R : never;
  687. };
  688. /**
  689. * Store augmented with getters. For internal usage only.
  690. * For internal use **only**
  691. */
  692. export declare type _StoreWithGetters<G> = {
  693. readonly [k in keyof G]: G[k] extends (...args: any[]) => infer R ? R : UnwrapRef<G[k]>;
  694. };
  695. /**
  696. * Base store with state and functions. Should not be used directly.
  697. */
  698. export declare interface _StoreWithState<Id extends string, S extends StateTree, G, A> extends StoreProperties<Id> {
  699. /**
  700. * State of the Store. Setting it will internally call `$patch()` to update the state.
  701. */
  702. $state: UnwrapRef<S> & PiniaCustomStateProperties<S>;
  703. /**
  704. * Applies a state patch to current state. Allows passing nested values
  705. *
  706. * @param partialState - patch to apply to the state
  707. */
  708. $patch(partialState: _DeepPartial<UnwrapRef<S>>): void;
  709. /**
  710. * Group multiple changes into one function. Useful when mutating objects like
  711. * Sets or arrays and applying an object patch isn't practical, e.g. appending
  712. * to an array. The function passed to `$patch()` **must be synchronous**.
  713. *
  714. * @param stateMutator - function that mutates `state`, cannot be asynchronous
  715. */
  716. $patch<F extends (state: UnwrapRef<S>) => any>(stateMutator: ReturnType<F> extends Promise<any> ? never : F): void;
  717. /**
  718. * Resets the store to its initial state by building a new state object.
  719. * TODO: make this options only
  720. */
  721. $reset(): void;
  722. /**
  723. * Setups a callback to be called whenever the state changes. It also returns a function to remove the callback. Note
  724. * that when calling `store.$subscribe()` inside of a component, it will be automatically cleaned up when the
  725. * component gets unmounted unless `detached` is set to true.
  726. *
  727. * @param callback - callback passed to the watcher
  728. * @param options - `watch` options + `detached` to detach the subscription from the context (usually a component)
  729. * this is called from. Note that the `flush` option does not affect calls to `store.$patch()`.
  730. * @returns function that removes the watcher
  731. */
  732. $subscribe(callback: SubscriptionCallback<S>, options?: {
  733. detached?: boolean;
  734. } & WatchOptions): () => void;
  735. /**
  736. * Setups a callback to be called every time an action is about to get
  737. * invoked. The callback receives an object with all the relevant information
  738. * of the invoked action:
  739. * - `store`: the store it is invoked on
  740. * - `name`: The name of the action
  741. * - `args`: The parameters passed to the action
  742. *
  743. * On top of these, it receives two functions that allow setting up a callback
  744. * once the action finishes or when it fails.
  745. *
  746. * It also returns a function to remove the callback. Note than when calling
  747. * `store.$onAction()` inside of a component, it will be automatically cleaned
  748. * up when the component gets unmounted unless `detached` is set to true.
  749. *
  750. * @example
  751. *
  752. *```js
  753. *store.$onAction(({ after, onError }) => {
  754. * // Here you could share variables between all of the hooks as well as
  755. * // setting up watchers and clean them up
  756. * after((resolvedValue) => {
  757. * // can be used to cleanup side effects
  758. * . // `resolvedValue` is the value returned by the action, if it's a
  759. * . // Promise, it will be the resolved value instead of the Promise
  760. * })
  761. * onError((error) => {
  762. * // can be used to pass up errors
  763. * })
  764. *})
  765. *```
  766. *
  767. * @param callback - callback called before every action
  768. * @param detached - detach the subscription from the context this is called from
  769. * @returns function that removes the watcher
  770. */
  771. $onAction(callback: StoreOnActionListener<Id, S, G, A>, detached?: boolean): () => void;
  772. /**
  773. * Stops the associated effect scope of the store and remove it from the store
  774. * registry. Plugins can override this method to cleanup any added effects.
  775. * e.g. devtools plugin stops displaying disposed stores from devtools.
  776. * Note this doesn't delete the state of the store, you have to do it manually with
  777. * `delete pinia.state.value[store.$id]` if you want to. If you don't and the
  778. * store is used again, it will reuse the previous state.
  779. */
  780. $dispose(): void;
  781. /* Excluded from this release type: _r */
  782. }
  783. /**
  784. * Callback of a subscription
  785. */
  786. export declare type SubscriptionCallback<S> = (
  787. /**
  788. * Object with information relative to the store mutation that triggered the
  789. * subscription.
  790. */
  791. mutation: SubscriptionCallbackMutation<S>,
  792. /**
  793. * State of the store when the subscription is triggered. Same as
  794. * `store.$state`.
  795. */
  796. state: UnwrapRef<S>) => void;
  797. /**
  798. * Context object passed to a subscription callback.
  799. */
  800. export declare type SubscriptionCallbackMutation<S> = SubscriptionCallbackMutationDirect | SubscriptionCallbackMutationPatchObject<S> | SubscriptionCallbackMutationPatchFunction;
  801. /**
  802. * Base type for the context passed to a subscription callback. Internal type.
  803. */
  804. export declare interface _SubscriptionCallbackMutationBase {
  805. /**
  806. * Type of the mutation.
  807. */
  808. type: MutationType;
  809. /**
  810. * `id` of the store doing the mutation.
  811. */
  812. storeId: string;
  813. /**
  814. * 🔴 DEV ONLY, DO NOT use for production code. Different mutation calls. Comes from
  815. * https://vuejs.org/guide/extras/reactivity-in-depth.html#reactivity-debugging and allows to track mutations in
  816. * devtools and plugins **during development only**.
  817. */
  818. events?: DebuggerEvent[] | DebuggerEvent;
  819. }
  820. /**
  821. * Context passed to a subscription callback when directly mutating the state of
  822. * a store with `store.someState = newValue` or `store.$state.someState =
  823. * newValue`.
  824. */
  825. export declare interface SubscriptionCallbackMutationDirect extends _SubscriptionCallbackMutationBase {
  826. type: MutationType.direct;
  827. events: DebuggerEvent;
  828. }
  829. /**
  830. * Context passed to a subscription callback when `store.$patch()` is called
  831. * with a function.
  832. */
  833. export declare interface SubscriptionCallbackMutationPatchFunction extends _SubscriptionCallbackMutationBase {
  834. type: MutationType.patchFunction;
  835. events: DebuggerEvent[];
  836. }
  837. /**
  838. * Context passed to a subscription callback when `store.$patch()` is called
  839. * with an object.
  840. */
  841. export declare interface SubscriptionCallbackMutationPatchObject<S> extends _SubscriptionCallbackMutationBase {
  842. type: MutationType.patchObject;
  843. events: DebuggerEvent[];
  844. /**
  845. * Object passed to `store.$patch()`.
  846. */
  847. payload: _DeepPartial<S>;
  848. }
  849. declare type ToComputedRefs<T> = {
  850. [K in keyof T]: ToRef<T[K]> extends Ref<infer U> ? ComputedRef<U> : ToRef<T[K]>;
  851. };
  852. /**
  853. * Type that enables refactoring through IDE.
  854. * For internal use **only**
  855. */
  856. export declare type _UnwrapAll<SS> = {
  857. [K in keyof SS]: UnwrapRef<SS[K]>;
  858. };
  859. export { }
  860. // Extensions of Vue types to be appended manually
  861. // https://github.com/microsoft/rushstack/issues/2090
  862. // https://github.com/microsoft/rushstack/issues/1709
  863. // @ts-ignore: works on Vue 2, fails in Vue 3
  864. declare module 'vue/types/vue' {
  865. interface Vue {
  866. /**
  867. * Currently installed pinia instance.
  868. */
  869. $pinia: Pinia
  870. /**
  871. * Cache of stores instantiated by the current instance. Used by map
  872. * helpers. Used internally by Pinia.
  873. *
  874. * @internal
  875. */
  876. _pStores?: Record<string, Store>
  877. }
  878. }
  879. // @ts-ignore: works on Vue 2, fails in Vue 3
  880. declare module 'vue/types/options' {
  881. interface ComponentOptions<V> {
  882. /**
  883. * Pinia instance to install in your application. Should be passed to the
  884. * root Vue.
  885. */
  886. pinia?: Pinia
  887. }
  888. }
  889. // TODO: figure out why it cannot be 'vue'
  890. // @ts-ignore: works on Vue 3, fails in Vue 2
  891. declare module '@vue/runtime-core' {
  892. export interface ComponentCustomProperties {
  893. /**
  894. * Access to the application's Pinia
  895. */
  896. $pinia: Pinia
  897. /**
  898. * Cache of stores instantiated by the current instance. Used by devtools to
  899. * list currently used stores. Used internally by Pinia.
  900. *
  901. * @internal
  902. */
  903. _pStores?: Record<string, StoreGeneric>
  904. }
  905. }