UNPKG

12.5 kBTypeScriptView Raw
1// Minimum TypeScript Version: 3.7
2
3/**
4 * This file is a manual translation of the flow types, which are the source of truth, so we should not introduce new terminology or behavior in this file.
5 */
6
7export { };
8
9 import * as React from 'react';
10
11// state.d.ts
12type NodeKey = string;
13
14// node.d.ts
15export class DefaultValue {
16 private __tag: 'DefaultValue';
17}
18
19// recoilRoot.d.ts
20export interface RecoilRootProps {
21 initializeState?: (mutableSnapshot: MutableSnapshot) => void;
22}
23
24export const RecoilRoot: React.FC<RecoilRootProps>;
25
26// Effect is called the first time a node is used with a <RecoilRoot>
27export type AtomEffect<T> = (param: {
28 node: RecoilState<T>,
29 trigger: 'set' | 'get',
30
31 // Call synchronously to initialize value or async to change it later
32 setSelf: (param:
33 | T
34 | DefaultValue
35 | Promise<T | DefaultValue>
36 | ((param: T | DefaultValue) => T | DefaultValue),
37 ) => void,
38 resetSelf: () => void,
39
40 // Subscribe callbacks to events.
41 // Atom effect observers are called before global transaction observers
42 onSet: (
43 param: (newValue: T | DefaultValue, oldValue: T | DefaultValue) => void,
44 ) => void,
45}) => void | (() => void);
46
47// atom.d.ts
48export interface AtomOptions<T> {
49 key: NodeKey;
50 default: RecoilValue<T> | Promise<T> | T;
51 effects_UNSTABLE?: ReadonlyArray<AtomEffect<T>>;
52 dangerouslyAllowMutability?: boolean;
53}
54
55/**
56 * Creates an atom, which represents a piece of writeable state
57 */
58export function atom<T>(options: AtomOptions<T>): RecoilState<T>;
59
60// selector.d.ts
61export type GetRecoilValue = <T>(recoilVal: RecoilValue<T>) => T;
62
63export type SetRecoilState = <T>(
64 recoilVal: RecoilState<T>,
65 newVal: T | DefaultValue | ((prevValue: T) => T | DefaultValue),
66) => void;
67
68export type ResetRecoilState = (recoilVal: RecoilState<any>) => void; // eslint-disable-line @typescript-eslint/no-explicit-any
69
70export interface ReadOnlySelectorOptions<T> {
71 key: string;
72 get: (opts: { get: GetRecoilValue }) => Promise<T> | RecoilValue<T> | T;
73 dangerouslyAllowMutability?: boolean;
74}
75
76export interface ReadWriteSelectorOptions<T> extends ReadOnlySelectorOptions<T> {
77 set: (
78 opts: {
79 set: SetRecoilState;
80 get: GetRecoilValue;
81 reset: ResetRecoilState;
82 },
83 newValue: T | DefaultValue,
84 ) => void;
85}
86
87export function selector<T>(options: ReadWriteSelectorOptions<T>): RecoilState<T>;
88export function selector<T>(options: ReadOnlySelectorOptions<T>): RecoilValueReadOnly<T>;
89
90// Snapshot.d.ts
91declare const SnapshotID_OPAQUE: unique symbol;
92export interface SnapshotID {
93 readonly [SnapshotID_OPAQUE]: true;
94}
95
96interface ComponentInfo {
97 name: string;
98}
99
100interface AtomInfo<T> {
101 loadable?: Loadable<T>;
102 isActive: boolean;
103 isSet: boolean;
104 isModified: boolean; // TODO report modified selectors
105 type: 'atom' | 'selector' | undefined; // undefined until initialized for now
106 deps: Iterable<RecoilValue<T>>;
107 subscribers: {
108 nodes: Iterable<RecoilValue<T>>,
109 components: Iterable<ComponentInfo>,
110 };
111}
112
113export class Snapshot {
114 getID(): SnapshotID;
115 getLoadable<T>(recoilValue: RecoilValue<T>): Loadable<T>;
116 getPromise<T>(recoilValue: RecoilValue<T>): Promise<T>;
117 getNodes_UNSTABLE(opts?: { isModified?: boolean }): Iterable<RecoilValue<unknown>>;
118 getInfo_UNSTABLE<T>(recoilValue: RecoilValue<T>): AtomInfo<T>;
119 map(cb: (mutableSnapshot: MutableSnapshot) => void): Snapshot;
120 asyncMap(cb: (mutableSnapshot: MutableSnapshot) => Promise<void>): Promise<Snapshot>;
121}
122
123export class MutableSnapshot extends Snapshot {
124 set: SetRecoilState;
125 reset: ResetRecoilState;
126}
127
128// hooks.d.ts
129export type SetterOrUpdater<T> = (valOrUpdater: ((currVal: T) => T) | T) => void;
130export type Resetter = () => void;
131export type CallbackInterface = Readonly<{
132 set: <T>(recoilVal: RecoilState<T>, valOrUpdater: ((currVal: T) => T) | T) => void;
133 reset: (recoilVal: RecoilState<any>) => void; // eslint-disable-line @typescript-eslint/no-explicit-any
134 snapshot: Snapshot,
135 gotoSnapshot: (snapshot: Snapshot) => void,
136}>;
137
138/**
139 * Returns the value of an atom or selector (readonly or writeable) and
140 * subscribes the components to future updates of that state.
141 */
142export function useRecoilValue<T>(recoilValue: RecoilValue<T>): T;
143
144/**
145 * Returns a Loadable representing the status of the given Recoil state
146 * and subscribes the component to future updates of that state. Useful
147 * for working with async selectors.
148 */
149export function useRecoilValueLoadable<T>(recoilValue: RecoilValue<T>): Loadable<T>;
150
151/**
152 * Returns a tuple where the first element is the value of the recoil state
153 * and the second is a setter to update that state. Subscribes component
154 * to updates of the given state.
155 */
156export function useRecoilState<T>(recoilState: RecoilState<T>): [T, SetterOrUpdater<T>];
157
158/**
159 * Returns a tuple where the first element is a Loadable and the second
160 * element is a setter function to update the given state. Subscribes
161 * component to updates of the given state.
162 */
163export function useRecoilStateLoadable<T>(recoilState: RecoilState<T>): [Loadable<T>, SetterOrUpdater<T>];
164
165/**
166 * Returns a setter function for updating Recoil state. Does not subscribe
167 * the component to the given state.
168 */
169
170export function useSetRecoilState<T>(recoilState: RecoilState<T>): SetterOrUpdater<T>;
171
172/**
173 * Returns a function that will reset the given state to its default value.
174 */
175export function useResetRecoilState(recoilState: RecoilState<any>): Resetter; // eslint-disable-line @typescript-eslint/no-explicit-any
176
177/**
178 * Returns current info about an atom
179 */
180export function useGetRecoilValueInfo_UNSTABLE<T>(recoilValue: RecoilValue<T>): AtomInfo<T>;
181
182/**
183 * Returns a function that will run the callback that was passed when
184 * calling this hook. Useful for accessing Recoil state in response to
185 * events.
186 */
187export function useRecoilCallback<Args extends ReadonlyArray<unknown>, Return>(
188 fn: (interface: CallbackInterface) => (...args: Args) => Return,
189 deps?: ReadonlyArray<unknown>,
190): (...args: Args) => Return;
191
192export function useRecoilTransactionObserver_UNSTABLE(
193 callback: (opts: {
194 snapshot: Snapshot,
195 previousSnapshot: Snapshot,
196 }) => void,
197): void;
198
199export function useGotoRecoilSnapshot(): (snapshot: Snapshot) => void;
200
201export function useRecoilSnapshot(): Snapshot;
202
203// useRecoilBridgeAcrossReactRoots.d.ts
204export const RecoilBridge: React.FC;
205export function useRecoilBridgeAcrossReactRoots_UNSTABLE(): typeof RecoilBridge;
206
207// loadable.d.ts
208declare const LoadablePromiseValue_OPAQUE: unique symbol;
209interface LoadablePromiseValue {
210 readonly [LoadablePromiseValue_OPAQUE]: true;
211}
212type LoadablePromise<T> = Promise<LoadablePromiseValue>;
213
214interface BaseLoadable<T> {
215 getValue: () => T;
216 toPromise: () => Promise<T>;
217 valueMaybe: () => T | void;
218 valueOrThrow: () => T;
219 errorMaybe: () => Error | void;
220 errorOrThrow: () => Error;
221 promiseMaybe: () => Promise<T> | void;
222 promiseOrThrow: () => Promise<T>;
223 map: <S>(map: (from: T) => Promise<S> | S) => Loadable<S>;
224}
225
226interface ValueLoadable<T> extends BaseLoadable<T> {
227 state: 'hasValue';
228 contents: T;
229}
230
231interface LoadingLoadable<T> extends BaseLoadable<T> {
232 state: 'loading';
233 contents: LoadablePromise<T>;
234}
235
236interface ErrorLoadable<T> extends BaseLoadable<T> {
237 state: 'hasError';
238 contents: Error;
239}
240
241export type Loadable<T> =
242 | ValueLoadable<T>
243 | LoadingLoadable<T>
244 | ErrorLoadable<T>;
245
246// recoilValue.d.ts
247declare class AbstractRecoilValue<T> {
248 __tag: [T];
249 __cTag: (t: T) => void; // for contravariance
250
251 key: NodeKey;
252 constructor(newKey: NodeKey);
253}
254
255declare class AbstractRecoilValueReadonly<T> {
256 __tag: [T];
257
258 key: NodeKey;
259 constructor(newKey: NodeKey);
260}
261
262export class RecoilState<T> extends AbstractRecoilValue<T> {}
263export class RecoilValueReadOnly<T> extends AbstractRecoilValueReadonly<T> {}
264export type RecoilValue<T> = RecoilValueReadOnly<T> | RecoilState<T>;
265
266export function isRecoilValue(val: unknown): val is RecoilValue<any>; // eslint-disable-line @typescript-eslint/no-explicit-any
267
268/** Utilities */
269
270// bigint not supported yet
271type Primitive = undefined | null | boolean | number | symbol | string;
272
273export type SerializableParam =
274 | Primitive
275 | {toJSON: () => string}
276 | ReadonlyArray<SerializableParam>
277 | Readonly<{[key: string]: SerializableParam}>;
278
279export interface AtomFamilyOptions<T, P extends SerializableParam> {
280 key: NodeKey;
281 dangerouslyAllowMutability?: boolean;
282 default: RecoilValue<T> | Promise<T> | T | ((param: P) => T | RecoilValue<T> | Promise<T>);
283 effects_UNSTABLE?: | ReadonlyArray<AtomEffect<T>> | ((param: P) => ReadonlyArray<AtomEffect<T>>);
284}
285
286export function atomFamily<T, P extends SerializableParam>(
287 options: AtomFamilyOptions<T, P>,
288): (param: P) => RecoilState<T>;
289
290export interface ReadOnlySelectorFamilyOptions<T, P extends SerializableParam> {
291 key: string;
292 get: (param: P) => (opts: { get: GetRecoilValue }) => Promise<T> | RecoilValue<T> | T;
293 // cacheImplementation_UNSTABLE?: () => CacheImplementation<Loadable<T>>,
294 // cacheImplementationForParams_UNSTABLE?: () => CacheImplementation<
295 // RecoilValue<T>,
296 // >,
297 dangerouslyAllowMutability?: boolean;
298}
299
300export interface ReadWriteSelectorFamilyOptions<T, P extends SerializableParam> {
301 key: string;
302 get: (param: P) => (opts: { get: GetRecoilValue }) => Promise<T> | RecoilValue<T> | T;
303 set: (
304 param: P,
305 ) => (
306 opts: { set: SetRecoilState; get: GetRecoilValue; reset: ResetRecoilState },
307 newValue: T | DefaultValue,
308 ) => void;
309 // cacheImplementation_UNSTABLE?: () => CacheImplementation<Loadable<T>>,
310 // cacheImplementationForParams_UNSTABLE?: () => CacheImplementation<
311 // RecoilValue<T>,
312 // >,
313 dangerouslyAllowMutability?: boolean;
314}
315
316export function selectorFamily<T, P extends SerializableParam>(
317 options: ReadWriteSelectorFamilyOptions<T, P>,
318): (param: P) => RecoilState<T>;
319
320export function selectorFamily<T, P extends SerializableParam>(
321 options: ReadOnlySelectorFamilyOptions<T, P>,
322): (param: P) => RecoilValueReadOnly<T>;
323
324export function constSelector<T extends SerializableParam>(constant: T): RecoilValueReadOnly<T>;
325
326export function errorSelector(message: string): RecoilValueReadOnly<never>;
327
328export function readOnlySelector<T>(atom: RecoilValue<T>): RecoilValueReadOnly<T>;
329
330export function noWait<T>(state: RecoilValue<T>): RecoilValueReadOnly<Loadable<T>>;
331
332/* eslint-disable @typescript-eslint/no-explicit-any */
333
334export type UnwrapRecoilValue<T> = T extends RecoilValue<infer R> ? R : never;
335
336export type UnwrapRecoilValues<T extends Array<RecoilValue<any>> | { [key: string]: RecoilValue<any> }> = {
337 [P in keyof T]: UnwrapRecoilValue<T[P]>;
338};
339export type UnwrapRecoilValueLoadables<T extends Array<RecoilValue<any>> | { [key: string]: RecoilValue<any> }> = {
340 [P in keyof T]: Loadable<UnwrapRecoilValue<T[P]>>;
341};
342
343export function waitForNone<RecoilValues extends Array<RecoilValue<any>> | [RecoilValue<any>]>(
344 param: RecoilValues,
345): RecoilValueReadOnly<UnwrapRecoilValueLoadables<RecoilValues>>;
346
347export function waitForNone<RecoilValues extends { [key: string]: RecoilValue<any> }>(
348 param: RecoilValues,
349): RecoilValueReadOnly<UnwrapRecoilValueLoadables<RecoilValues>>;
350
351export function waitForAny<RecoilValues extends Array<RecoilValue<any>> | [RecoilValue<any>]>(
352 param: RecoilValues,
353): RecoilValueReadOnly<UnwrapRecoilValueLoadables<RecoilValues>>;
354
355export function waitForAny<RecoilValues extends { [key: string]: RecoilValue<any> }>(
356 param: RecoilValues,
357): RecoilValueReadOnly<UnwrapRecoilValueLoadables<RecoilValues>>;
358
359export function waitForAll<RecoilValues extends Array<RecoilValue<any>> | [RecoilValue<any>]>(
360 param: RecoilValues,
361): RecoilValueReadOnly<UnwrapRecoilValues<RecoilValues>>;
362
363export function waitForAll<RecoilValues extends { [key: string]: RecoilValue<any> }>(
364 param: RecoilValues,
365): RecoilValueReadOnly<UnwrapRecoilValues<RecoilValues>>;
366
367export function waitForAllSettled<RecoilValues extends Array<RecoilValue<any>> | [RecoilValue<any>]>(
368 param: RecoilValues,
369): RecoilValueReadOnly<UnwrapRecoilValueLoadables<RecoilValues>>;
370
371export function waitForAllSettled<RecoilValues extends { [key: string]: RecoilValue<any> }>(
372 param: RecoilValues,
373): RecoilValueReadOnly<UnwrapRecoilValueLoadables<RecoilValues>>;
374
375/* eslint-enable @typescript-eslint/no-explicit-any */
376
377export function snapshot_UNSTABLE(initializeState?: (shapshot: MutableSnapshot) => void): Snapshot;