Skip to content

Types ​

Pinia ​

The Pinia instance type (returned by createPinia).

MemberSignatureNotes
use(plugin: PiniaPlugin) => Piniaregisters a plugin, chainable
usePersistPlugin(options: PersistOptions) => Piniabridge-safe persistence-plugin registration (chainable)
install(app: any) => voidentry for app.use(pinia) (reliable on Web only; use setActivePinia on native)

PiniaStoreBase ​

The class-style store base class; all stores inherit it to gain the $-API and persistence capabilities.

MemberSignatureNotes
$idstringstore id (injected when defineStore creates the instance)
$stateany | nullreference to the current state (same reference as the subclass state after bindState)
bindState(state: any) => voidcalled once after super() in the constructor to bind the reactive state
$patch(partial: UTSJSONObject) => voidbatch update (loaded via _hydrate) and notify subscribers
$reset() => voidruns the _doReset hook, then notifies subscribers
$save() => voidexplicit save point (persist-to-storage trigger after direct state mutations)
$subscribe(cb: StateChangeCallback) => Unsubscribesubscribe to state changes (triggered by $patch / $reset / $save)
$onAction(cb: ActionChangeCallback) => Unsubscribesubscribe to action calls (actions wrapped with callAction)
$dispose() => voiddispose the instance; the next useStore() rebuilds it
callAction(name: string, fn: () => any) => any | nullmakes an action trigger $onAction subscribers
_doReset() => voidhook: defines $reset behavior (override on demand)
_hydrate(data: UTSJSONObject) => voidhook: loads partial data / persistence restore (override on demand)
_serialize() => UTSJSONObjecthook: state snapshot (override on demand)

PiniaPlugin ​

uts
export type PiniaPlugin = (ctx: PiniaPluginContext) => void

Plugin function type. UTS constraint: returns void (cannot return an object to merge into the store); plugin logic goes through ctx.

PiniaPluginContext ​

uts
export type PiniaPluginContext = {
  pinia: Pinia,
  store: any,
  storeId: string
}
FieldNotes
piniathe current Pinia instance
storethe newly created store instance (any; narrow with as any as PiniaStoreBase at the use site)
storeIdthe store id

StateChangeCallback ​

uts
export type StateChangeCallback = (type: string, state: UTSJSONObject) => void

$subscribe callback: type = 'patch' | 'reset' | 'save'; state = the _serialize() snapshot.

ActionInfo / ActionChangeCallback ​

uts
export type ActionInfo = {
  name: string
}

export type ActionChangeCallback = (info: ActionInfo) => void

$onAction callback data: only actions wrapped with callAction / callActionWithArgs are tracked.

Unsubscribe ​

uts
export type Unsubscribe = () => void

Unsubscribe function (the return value of $subscribe / $onAction).

StorageAdapter ​

uts
export type StorageAdapter = {
  get: ((key: string) => string | null) | null,
  set: ((key: string, value: string) => void) | null,
  remove: ((key: string) => void) | null
}

Storage adapter (persistence backend): the default implementation is based on uni storage (zero dependencies); swap to unix-storage / native preferences. The three callback fields are optional (null) — omitted callbacks fall back to default uni storage behavior; the nullable declaration gives the bridge conversion null guards.

PersistOptions ​

uts
export type PersistOptions = {
  keyPrefix: string,
  includeStores: Array<string> | null,
  excludeStores: Array<string>,
  serializer: ((state: UTSJSONObject) => string) | null
}
FieldNotes
keyPrefixstorage key prefix (full key = keyPrefix + storeId), default 'ux-pinia:'
includeStorespersist only the given stores (null = all)
excludeStoresexclude the given stores
serializercustom serializer (null = JSON serialization)

UTS types must be complete

UTS object literals must be complete: when calling createPersistPlugin, all four fields must be given explicitly (optional semantics are expressed with null); see the Basic usage example.

Store type convention ​

A store = a PiniaStoreBase subclass + a standalone state type:

uts
export type CounterState = {
  count: number
  name: string
}

export class CounterStore extends PiniaStoreBase {
  state: CounterState = reactive<CounterState>({ count: 0, name: 'pinia' })

  constructor() {
    super()
    this.bindState(this.state)
  }
}

Released under the MIT License.