UNPKG

2.39 kBTypeScriptView Raw
1import type { SetStateAction, WritableAtom } from 'jotai';
2import { RESET } from './constants';
3declare type Unsubscribe = () => void;
4declare type AsyncStorage<Value> = {
5 getItem: (key: string) => Promise<Value>;
6 setItem: (key: string, newValue: Value) => Promise<void>;
7 removeItem: (key: string) => Promise<void>;
8 delayInit?: boolean;
9 subscribe?: (key: string, callback: (value: Value) => void) => Unsubscribe;
10};
11declare type SyncStorage<Value> = {
12 getItem: (key: string) => Value;
13 setItem: (key: string, newValue: Value) => void;
14 removeItem: (key: string) => void;
15 delayInit?: boolean;
16 subscribe?: (key: string, callback: (value: Value) => void) => Unsubscribe;
17};
18declare type AsyncStringStorage = {
19 getItem: (key: string) => Promise<string | null>;
20 setItem: (key: string, newValue: string) => Promise<void>;
21 removeItem: (key: string) => Promise<void>;
22};
23declare type SyncStringStorage = {
24 getItem: (key: string) => string | null;
25 setItem: (key: string, newValue: string) => void;
26 removeItem: (key: string) => void;
27};
28export declare function createJSONStorage<Value>(getStringStorage: () => AsyncStringStorage): AsyncStorage<Value>;
29export declare function createJSONStorage<Value>(getStringStorage: () => SyncStringStorage): SyncStorage<Value>;
30export declare function atomWithStorage<Value>(key: string, initialValue: Value, storage: AsyncStorage<Value> & {
31 delayInit: true;
32}): WritableAtom<Value, SetStateAction<Value> | typeof RESET, Promise<void>>;
33export declare function atomWithStorage<Value>(key: string, initialValue: Value, storage: AsyncStorage<Value>): WritableAtom<Promise<Value>, SetStateAction<Value> | typeof RESET, Promise<void>>;
34export declare function atomWithStorage<Value>(key: string, initialValue: Value, storage: SyncStorage<Value>): WritableAtom<Value, SetStateAction<Value> | typeof RESET>;
35export declare function atomWithStorage<Value>(key: string, initialValue: Value): WritableAtom<Value, SetStateAction<Value> | typeof RESET>;
36export declare function atomWithHash<Value>(key: string, initialValue: Value, options?: {
37 serialize?: (val: Value) => string;
38 deserialize?: (str: string) => Value;
39 delayInit?: boolean;
40 replaceState?: boolean;
41 subscribe?: (callback: () => void) => () => void;
42}): WritableAtom<Value, SetStateAction<Value> | typeof RESET>;
43export {};