UNPKG

11.8 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
96export class Snapshot {
97 getID(): SnapshotID;
98 getLoadable<T>(recoilValue: RecoilValue<T>): Loadable<T>;
99 getPromise<T>(recoilValue: RecoilValue<T>): Promise<T>;
100 getNodes_UNSTABLE(opts?: { isModified?: boolean }): Iterable<RecoilValue<unknown>>;
101 getInfo_UNSTABLE<T>(recoilValue: RecoilValue<T>): {
102 loadable?: Loadable<T>,
103 isActive: boolean,
104 isSet: boolean,
105 isModified: boolean, // TODO report modified selectors
106 type: 'atom' | 'selector' | undefined, // undefined until initialized for now
107 deps: Iterable<RecoilValue<T>>,
108 subscribers: {
109 nodes: Iterable<RecoilValue<T>>,
110 },
111 };
112 map(cb: (mutableSnapshot: MutableSnapshot) => void): Snapshot;
113 asyncMap(cb: (mutableSnapshot: MutableSnapshot) => Promise<void>): Promise<Snapshot>;
114}
115
116export class MutableSnapshot extends Snapshot {
117 set: SetRecoilState;
118 reset: ResetRecoilState;
119}
120
121// hooks.d.ts
122export type SetterOrUpdater<T> = (valOrUpdater: ((currVal: T) => T) | T) => void;
123export type Resetter = () => void;
124export type CallbackInterface = Readonly<{
125 set: <T>(recoilVal: RecoilState<T>, valOrUpdater: ((currVal: T) => T) | T) => void;
126 reset: (recoilVal: RecoilState<any>) => void; // eslint-disable-line @typescript-eslint/no-explicit-any
127 snapshot: Snapshot,
128 gotoSnapshot: (snapshot: Snapshot) => void,
129}>;
130
131/**
132 * Returns the value of an atom or selector (readonly or writeable) and
133 * subscribes the components to future updates of that state.
134 */
135export function useRecoilValue<T>(recoilValue: RecoilValue<T>): T;
136
137/**
138 * Returns a Loadable representing the status of the given Recoil state
139 * and subscribes the component to future updates of that state. Useful
140 * for working with async selectors.
141 */
142export function useRecoilValueLoadable<T>(recoilValue: RecoilValue<T>): Loadable<T>;
143
144/**
145 * Returns a tuple where the first element is the value of the recoil state
146 * and the second is a setter to update that state. Subscribes component
147 * to updates of the given state.
148 */
149export function useRecoilState<T>(recoilState: RecoilState<T>): [T, SetterOrUpdater<T>];
150
151/**
152 * Returns a tuple where the first element is a Loadable and the second
153 * element is a setter function to update the given state. Subscribes
154 * component to updates of the given state.
155 */
156export function useRecoilStateLoadable<T>(recoilState: RecoilState<T>): [Loadable<T>, SetterOrUpdater<T>];
157
158/**
159 * Returns a setter function for updating Recoil state. Does not subscribe
160 * the component to the given state.
161 */
162
163export function useSetRecoilState<T>(recoilState: RecoilState<T>): SetterOrUpdater<T>;
164
165/**
166 * Returns a function that will reset the given state to its default value.
167 */
168export function useResetRecoilState(recoilState: RecoilState<any>): Resetter; // eslint-disable-line @typescript-eslint/no-explicit-any
169
170/**
171 * Returns a function that will run the callback that was passed when
172 * calling this hook. Useful for accessing Recoil state in response to
173 * events.
174 */
175export function useRecoilCallback<Args extends ReadonlyArray<unknown>, Return>(
176 fn: (interface: CallbackInterface) => (...args: Args) => Return,
177 deps?: ReadonlyArray<unknown>,
178): (...args: Args) => Return;
179
180export function useRecoilTransactionObserver_UNSTABLE(
181 callback: (opts: {
182 snapshot: Snapshot,
183 previousSnapshot: Snapshot,
184 }) => void,
185): void;
186
187export function useGotoRecoilSnapshot(): (snapshot: Snapshot) => void;
188
189export function useRecoilSnapshot(): Snapshot;
190
191// useRecoilBridgeAcrossReactRoots.d.ts
192export const RecoilBridge: React.FC;
193export function useRecoilBridgeAcrossReactRoots_UNSTABLE(): typeof RecoilBridge;
194
195// loadable.d.ts
196declare const LoadablePromiseValue_OPAQUE: unique symbol;
197interface LoadablePromiseValue {
198 readonly [LoadablePromiseValue_OPAQUE]: true;
199}
200type LoadablePromise<T> = Promise<LoadablePromiseValue>;
201
202interface BaseLoadable<T> {
203 getValue: () => T;
204 toPromise: () => Promise<T>;
205 valueMaybe: () => T | void;
206 valueOrThrow: () => T;
207 errorMaybe: () => Error | void;
208 errorOrThrow: () => Error;
209 promiseMaybe: () => Promise<T> | void;
210 promiseOrThrow: () => Promise<T>;
211 map: <S>(map: (from: T) => Promise<S> | S) => Loadable<S>;
212}
213
214interface ValueLoadable<T> extends BaseLoadable<T> {
215 state: 'hasValue';
216 contents: T;
217}
218
219interface LoadingLoadable<T> extends BaseLoadable<T> {
220 state: 'loading';
221 contents: LoadablePromise<T>;
222}
223
224interface ErrorLoadable<T> extends BaseLoadable<T> {
225 state: 'hasError';
226 contents: Error;
227}
228
229export type Loadable<T> =
230 | ValueLoadable<T>
231 | LoadingLoadable<T>
232 | ErrorLoadable<T>;
233
234// recoilValue.d.ts
235declare class AbstractRecoilValue<T> {
236 __tag: [T];
237 __cTag: (t: T) => void; // for contravariance
238
239 key: NodeKey;
240 constructor(newKey: NodeKey);
241}
242
243declare class AbstractRecoilValueReadonly<T> {
244 __tag: [T];
245
246 key: NodeKey;
247 constructor(newKey: NodeKey);
248}
249
250export class RecoilState<T> extends AbstractRecoilValue<T> {}
251export class RecoilValueReadOnly<T> extends AbstractRecoilValueReadonly<T> {}
252export type RecoilValue<T> = RecoilValueReadOnly<T> | RecoilState<T>;
253
254export function isRecoilValue(val: unknown): val is RecoilValue<any>; // eslint-disable-line @typescript-eslint/no-explicit-any
255
256/** Utilities */
257
258// bigint not supported yet
259type Primitive = undefined | null | boolean | number | symbol | string;
260
261export type SerializableParam = Primitive | ReadonlyArray<SerializableParam> | Readonly<{[key: string]: SerializableParam}>;
262
263export interface AtomFamilyOptions<T, P extends SerializableParam> {
264 key: NodeKey;
265 dangerouslyAllowMutability?: boolean;
266 default: RecoilValue<T> | Promise<T> | T | ((param: P) => T | RecoilValue<T> | Promise<T>);
267 effects_UNSTABLE?: | ReadonlyArray<AtomEffect<T>> | ((param: P) => ReadonlyArray<AtomEffect<T>>);
268}
269
270export function atomFamily<T, P extends SerializableParam>(
271 options: AtomFamilyOptions<T, P>,
272): (param: P) => RecoilState<T>;
273
274export interface ReadOnlySelectorFamilyOptions<T, P extends SerializableParam> {
275 key: string;
276 get: (param: P) => (opts: { get: GetRecoilValue }) => Promise<T> | RecoilValue<T> | T;
277 // cacheImplementation_UNSTABLE?: () => CacheImplementation<Loadable<T>>,
278 // cacheImplementationForParams_UNSTABLE?: () => CacheImplementation<
279 // RecoilValue<T>,
280 // >,
281 dangerouslyAllowMutability?: boolean;
282}
283
284export interface ReadWriteSelectorFamilyOptions<T, P extends SerializableParam> {
285 key: string;
286 get: (param: P) => (opts: { get: GetRecoilValue }) => Promise<T> | RecoilValue<T> | T;
287 set: (
288 param: P,
289 ) => (
290 opts: { set: SetRecoilState; get: GetRecoilValue; reset: ResetRecoilState },
291 newValue: T | DefaultValue,
292 ) => void;
293 // cacheImplementation_UNSTABLE?: () => CacheImplementation<Loadable<T>>,
294 // cacheImplementationForParams_UNSTABLE?: () => CacheImplementation<
295 // RecoilValue<T>,
296 // >,
297 dangerouslyAllowMutability?: boolean;
298}
299
300export function selectorFamily<T, P extends SerializableParam>(
301 options: ReadWriteSelectorFamilyOptions<T, P>,
302): (param: P) => RecoilState<T>;
303
304export function selectorFamily<T, P extends SerializableParam>(
305 options: ReadOnlySelectorFamilyOptions<T, P>,
306): (param: P) => RecoilValueReadOnly<T>;
307
308export function constSelector<T extends SerializableParam>(constant: T): RecoilValueReadOnly<T>;
309
310export function errorSelector(message: string): RecoilValueReadOnly<never>;
311
312export function readOnlySelector<T>(atom: RecoilValue<T>): RecoilValueReadOnly<T>;
313
314export function noWait<T>(state: RecoilValue<T>): RecoilValueReadOnly<Loadable<T>>;
315
316/* eslint-disable @typescript-eslint/no-explicit-any */
317
318export type UnwrapRecoilValue<T> = T extends RecoilValue<infer R> ? R : never;
319
320export type UnwrapRecoilValues<T extends Array<RecoilValue<any>> | { [key: string]: RecoilValue<any> }> = {
321 [P in keyof T]: UnwrapRecoilValue<T[P]>;
322};
323export type UnwrapRecoilValueLoadables<T extends Array<RecoilValue<any>> | { [key: string]: RecoilValue<any> }> = {
324 [P in keyof T]: Loadable<UnwrapRecoilValue<T[P]>>;
325};
326
327export function waitForNone<RecoilValues extends Array<RecoilValue<any>> | [RecoilValue<any>]>(
328 param: RecoilValues,
329): RecoilValueReadOnly<UnwrapRecoilValueLoadables<RecoilValues>>;
330
331export function waitForNone<RecoilValues extends { [key: string]: RecoilValue<any> }>(
332 param: RecoilValues,
333): RecoilValueReadOnly<UnwrapRecoilValueLoadables<RecoilValues>>;
334
335export function waitForAny<RecoilValues extends Array<RecoilValue<any>> | [RecoilValue<any>]>(
336 param: RecoilValues,
337): RecoilValueReadOnly<UnwrapRecoilValueLoadables<RecoilValues>>;
338
339export function waitForAny<RecoilValues extends { [key: string]: RecoilValue<any> }>(
340 param: RecoilValues,
341): RecoilValueReadOnly<UnwrapRecoilValueLoadables<RecoilValues>>;
342
343export function waitForAll<RecoilValues extends Array<RecoilValue<any>> | [RecoilValue<any>]>(
344 param: RecoilValues,
345): RecoilValueReadOnly<UnwrapRecoilValues<RecoilValues>>;
346
347export function waitForAll<RecoilValues extends { [key: string]: RecoilValue<any> }>(
348 param: RecoilValues,
349): RecoilValueReadOnly<UnwrapRecoilValues<RecoilValues>>;
350
351/* eslint-enable @typescript-eslint/no-explicit-any */
352
353export function snapshot_UNSTABLE(initializeState?: (shapshot: MutableSnapshot) => void): Snapshot;