UNPKG

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