zustand
Version:
🐻 Bear necessities for state management in React
90 lines (89 loc) • 3.7 kB
TypeScript
import { StateCreator, StoreMutatorIdentifier } from '../vanilla';
export interface StateStorage {
getItem: (name: string) => string | null | Promise<string | null>;
setItem: (name: string, value: string) => void | Promise<void>;
removeItem: (name: string) => void | Promise<void>;
}
declare type StorageValue<S> = {
state: S;
version?: number;
};
export interface PersistOptions<S, PersistedState = S> {
/** Name of the storage (must be unique) */
name: string;
/**
* A function returning a storage.
* The storage must fit `window.localStorage`'s api (or an async version of it).
* For example the storage could be `AsyncStorage` from React Native.
*
* @default () => localStorage
*/
getStorage?: () => StateStorage;
/**
* Use a custom serializer.
* The returned string will be stored in the storage.
*
* @default JSON.stringify
*/
serialize?: (state: StorageValue<S>) => string | Promise<string>;
/**
* Use a custom deserializer.
* Must return an object matching StorageValue<S>
*
* @param str The storage's current value.
* @default JSON.parse
*/
deserialize?: (str: string) => StorageValue<PersistedState> | Promise<StorageValue<PersistedState>>;
/**
* Filter the persisted value.
*
* @params state The state's value
*/
partialize?: (state: S) => PersistedState;
/**
* A function returning another (optional) function.
* The main function will be called before the state rehydration.
* The returned function will be called after the state rehydration or when an error occurred.
*/
onRehydrateStorage?: (state: S) => ((state?: S, error?: unknown) => void) | void;
/**
* If the stored state's version mismatch the one specified here, the storage will not be used.
* This is useful when adding a breaking change to your store.
*/
version?: number;
/**
* A function to perform persisted state migration.
* This function will be called when persisted state versions mismatch with the one specified here.
*/
migrate?: (persistedState: unknown, version: number) => S | Promise<S>;
/**
* A function to perform custom hydration merges when combining the stored state with the current one.
* By default, this function does a shallow merge.
*/
merge?: (persistedState: unknown, currentState: S) => S;
}
declare type PersistListener<S> = (state: S) => void;
declare type StorePersist<S extends object, Ps> = {
persist: {
setOptions: (options: Partial<PersistOptions<S, Ps>>) => void;
clearStorage: () => void;
rehydrate: () => Promise<void>;
hasHydrated: () => boolean;
onHydrate: (fn: PersistListener<S>) => () => void;
onFinishHydration: (fn: PersistListener<S>) => () => void;
getOptions: () => Partial<PersistOptions<S, Ps>>;
};
};
declare type Persist = <T extends object, Mps extends [StoreMutatorIdentifier, unknown][] = [], Mcs extends [StoreMutatorIdentifier, unknown][] = [], U = T>(initializer: StateCreator<T, [...Mps, ['zustand/persist', unknown]], Mcs>, options?: PersistOptions<T, U>) => StateCreator<T, Mps, [['zustand/persist', U], ...Mcs]>;
declare module '../vanilla' {
interface StoreMutators<S, A> {
'zustand/persist': WithPersist<S, A>;
}
}
declare type Write<T extends object, U extends object> = Omit<T, keyof U> & U;
declare type Cast<T, U> = T extends U ? T : U;
declare type WithPersist<S, A> = S extends {
getState: () => infer T;
} ? Write<S, StorePersist<Cast<T, object>, A>> : never;
export declare const persist: Persist;
export {};