UNPKG

2.65 kBTypeScriptView Raw
1declare type Awaited<T> = T extends Promise<infer V> ? Awaited<V> : T;
2declare type Getter = {
3 <Value>(atom: Atom<Value | Promise<Value>>): Value;
4 <Value>(atom: Atom<Promise<Value>>): Value;
5 <Value>(atom: Atom<Value>): Awaited<Value>;
6};
7declare type WriteGetter = Getter & {
8 <Value>(atom: Atom<Value | Promise<Value>>, options: {
9 unstable_promise: true;
10 }): Promise<Value> | Value;
11 <Value>(atom: Atom<Promise<Value>>, options: {
12 unstable_promise: true;
13 }): Promise<Value> | Value;
14 <Value>(atom: Atom<Value>, options: {
15 unstable_promise: true;
16 }): Promise<Awaited<Value>> | Awaited<Value>;
17};
18declare type Setter = {
19 <Value, Result extends void | Promise<void>>(atom: WritableAtom<Value, undefined, Result>): Result;
20 <Value, Update, Result extends void | Promise<void>>(atom: WritableAtom<Value, Update, Result>, update: Update): Result;
21};
22declare type Read<Value> = (get: Getter) => Value;
23declare type Write<Update, Result extends void | Promise<void>> = (get: WriteGetter, set: Setter, update: Update) => Result;
24declare type WithInitialValue<Value> = {
25 init: Value;
26};
27export declare type Scope = symbol | string | number;
28export declare type SetAtom<Update, Result extends void | Promise<void>> = undefined extends Update ? (update?: Update) => Result : (update: Update) => Result;
29declare type OnUnmount = () => void;
30declare type OnMount<Update, Result extends void | Promise<void>> = <S extends SetAtom<Update, Result>>(setAtom: S) => OnUnmount | void;
31export declare type Atom<Value> = {
32 toString: () => string;
33 debugLabel?: string;
34 read: Read<Value>;
35};
36export declare type WritableAtom<Value, Update, Result extends void | Promise<void> = void> = Atom<Value> & {
37 write: Write<Update, Result>;
38 onMount?: OnMount<Update, Result>;
39};
40declare type SetStateAction<Value> = Value | ((prev: Value) => Value);
41export declare type PrimitiveAtom<Value> = WritableAtom<Value, SetStateAction<Value>>;
42export declare function atom<Value, Update, Result extends void | Promise<void> = void>(read: Read<Value>, write: Write<Update, Result>): WritableAtom<Value, Update, Result>;
43export declare function atom<Value>(read: Read<Value>): Atom<Value>;
44export declare function atom(invalidFunction: (...args: any) => any, write?: any): never;
45export declare function atom<Value, Update, Result extends void | Promise<void> = void>(initialValue: Value, write: Write<Update, Result>): WritableAtom<Value, Update, Result> & WithInitialValue<Value>;
46export declare function atom<Value>(initialValue: Value): PrimitiveAtom<Value> & WithInitialValue<Value>;
47export {};
48
\No newline at end of file