UNPKG

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