UNPKG

1.85 kBTypeScriptView Raw
1type Getter = <Value>(atom: Atom<Value>) => Value;
2type Setter = <Value, Args extends unknown[], Result>(atom: WritableAtom<Value, Args, Result>, ...args: Args) => Result;
3type SetAtom<Args extends unknown[], Result> = <A extends Args>(...args: A) => Result;
4type Read<Value, SetSelf = never> = (get: Getter, options: {
5 readonly signal: AbortSignal;
6 readonly setSelf: SetSelf;
7}) => Value;
8type Write<Args extends unknown[], Result> = (get: Getter, set: Setter, ...args: Args) => Result;
9type WithInitialValue<Value> = {
10 init: Value;
11};
12type OnUnmount = () => void;
13type OnMount<Args extends unknown[], Result> = <S extends SetAtom<Args, Result>>(setAtom: S) => OnUnmount | void;
14export interface Atom<Value> {
15 toString: () => string;
16 read: Read<Value>;
17 debugLabel?: string;
18 /**
19 * To ONLY be used by Jotai libraries to mark atoms as private. Subject to change.
20 * @private
21 */
22 debugPrivate?: boolean;
23}
24export interface WritableAtom<Value, Args extends unknown[], Result> extends Atom<Value> {
25 read: Read<Value, SetAtom<Args, Result>>;
26 write: Write<Args, Result>;
27 onMount?: OnMount<Args, Result>;
28}
29type SetStateAction<Value> = Value | ((prev: Value) => Value);
30export type PrimitiveAtom<Value> = WritableAtom<Value, [
31 SetStateAction<Value>
32], void>;
33export declare function atom<Value, Args extends unknown[], Result>(read: Read<Value, SetAtom<Args, Result>>, write: Write<Args, Result>): WritableAtom<Value, Args, Result>;
34export declare function atom<Value>(read: Read<Value>): Atom<Value>;
35export declare function atom<Value, Args extends unknown[], Result>(initialValue: Value, write: Write<Args, Result>): WritableAtom<Value, Args, Result> & WithInitialValue<Value>;
36export declare function atom<Value>(initialValue: Value): PrimitiveAtom<Value> & WithInitialValue<Value>;
37export {};
38
\No newline at end of file