UNPKG

1.86 kBTypeScriptView Raw
1export declare type Getter = {
2 <Value>(atom: Atom<Value | Promise<Value>>): Value;
3 <Value>(atom: Atom<Promise<Value>>): Value;
4 <Value>(atom: Atom<Value>): Value;
5};
6export declare type Setter = {
7 <Value>(atom: WritableAtom<Value, undefined>): void;
8 <Value, Update>(atom: WritableAtom<Value, Update>, update: Update): void;
9};
10declare type Read<Value> = (get: Getter) => Value | Promise<Value>;
11declare type Write<Update> = (get: Getter, set: Setter, update: Update) => void | Promise<void>;
12declare type WithInitialValue<Value> = {
13 init: Value;
14};
15export declare type Scope = symbol | string | number;
16export declare type SetAtom<Update> = undefined extends Update ? (update?: Update) => void | Promise<void> : (update: Update) => void | Promise<void>;
17declare type OnUnmount = () => void;
18declare type OnMount<Update> = <S extends SetAtom<Update>>(setAtom: S) => OnUnmount | void;
19export declare type Atom<Value> = {
20 toString: () => string;
21 debugLabel?: string;
22 scope?: Scope;
23 read: Read<Value>;
24};
25export declare type WritableAtom<Value, Update> = Atom<Value> & {
26 write: Write<Update>;
27 onMount?: OnMount<Update>;
28};
29declare type SetStateAction<Value> = Value | ((prev: Value) => Value);
30export declare type PrimitiveAtom<Value> = WritableAtom<Value, SetStateAction<Value>>;
31export declare function atom<Value, Update>(read: Read<Value>, write: Write<Update>): WritableAtom<Value, Update>;
32export declare function atom<Value, Update>(initialValue: Value, write: Write<Update>): [Value] extends [Function] ? never : WritableAtom<Value, Update> & WithInitialValue<Value>;
33export declare function atom<Value>(read: Read<Value>): Atom<Value>;
34export declare function atom<Value extends unknown>(initialValue: Value): [Value] extends [Function] ? never : PrimitiveAtom<Value> & WithInitialValue<Value>;
35export {};
36
\No newline at end of file