UNPKG

9.52 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// state.d.ts
10type NodeKey = string;
11type AtomValues = Map<NodeKey, Loadable<any>>;
12type ComponentCallback = (state: TreeState) => void;
13type TreeState = Readonly<{
14 isSnapshot: boolean;
15 transactionMetadata: object;
16 dirtyAtoms: Set<NodeKey>;
17 atomValues: AtomValues;
18 nonvalidatedAtoms: Map<NodeKey, unknown>;
19 nodeDeps: Map<NodeKey, Set<NodeKey>>;
20 nodeToNodeSubscriptions: Map<NodeKey, Set<NodeKey>>;
21 nodeToComponentSubscriptions: Map<NodeKey, Map<number, [string, ComponentCallback]>>;
22}>;
23
24// node.d.ts
25export class DefaultValue {}
26
27// recoilRoot.d.ts
28import * as React from 'react';
29
30export interface RecoilRootProps {
31 initializeState?: (mutableSnapshot: MutableSnapshot) => void;
32}
33
34export const RecoilRoot: React.FC<RecoilRootProps>;
35
36// atom.d.ts
37export interface AtomOptions<T> {
38 key: NodeKey;
39 default: RecoilValue<T> | Promise<T> | T;
40 dangerouslyAllowMutability?: boolean;
41}
42/**
43 * Creates an atom, which represents a piece of writeable state
44 */
45export function atom<T>(options: AtomOptions<T>): RecoilState<T>;
46
47// selector.d.ts
48type GetRecoilValue = <T>(recoilVal: RecoilValue<T>) => T;
49
50type SetRecoilState = <T>(
51 recoilVal: RecoilState<T>,
52 newVal: T | DefaultValue | ((prevValue: T) => T | DefaultValue),
53) => void;
54
55type ResetRecoilState = (recoilVal: RecoilState<any>) => void;
56
57export interface ReadOnlySelectorOptions<T> {
58 key: string;
59 get: (opts: { get: GetRecoilValue }) => Promise<T> | RecoilValue<T> | T;
60 dangerouslyAllowMutability?: boolean;
61}
62
63export interface ReadWriteSelectorOptions<T> extends ReadOnlySelectorOptions<T> {
64 set: (
65 opts: {
66 set: SetRecoilState;
67 get: GetRecoilValue;
68 reset: ResetRecoilState;
69 },
70 newValue: T | DefaultValue,
71 ) => void;
72}
73
74export function selector<T>(options: ReadWriteSelectorOptions<T>): RecoilState<T>;
75export function selector<T>(options: ReadOnlySelectorOptions<T>): RecoilValueReadOnly<T>;
76
77// hooks.d.ts
78export class Snapshot {
79 getLoadable<T>(recoilValue: RecoilValue<T>): Loadable<T>;
80 getPromise<T>(recoilValue: RecoilValue<T>): Promise<T>;
81 map(cb: (mutableSnapshot: MutableSnapshot) => void): Snapshot;
82 asyncMap(cb: (mutableSnapshot: MutableSnapshot) => Promise<void>): Promise<Snapshot>;
83}
84
85export class MutableSnapshot extends Snapshot {
86 set: SetRecoilState;
87 reset: ResetRecoilState;
88}
89
90export type SetterOrUpdater<T> = (valOrUpdater: ((currVal: T) => T) | T) => void;
91export type Resetter = () => void;
92export type CallbackInterface = Readonly<{
93 set: <T>(recoilVal: RecoilState<T>, valOrUpdater: ((currVal: T) => T) | T) => void;
94 reset: (recoilVal: RecoilState<any>) => void;
95 snapshot: Snapshot,
96 gotoSnapshot: (snapshot: Snapshot) => void,
97}>;
98
99/**
100 * Returns the value of an atom or selector (readonly or writeable) and
101 * subscribes the components to future updates of that state.
102 */
103export function useRecoilValue<T>(recoilValue: RecoilValue<T>): T;
104
105/**
106 * Returns a Loadable representing the status of the given Recoil state
107 * and subscribes the component to future updates of that state. Useful
108 * for working with async selectors.
109 */
110export function useRecoilValueLoadable<T>(recoilValue: RecoilValue<T>): Loadable<T>;
111
112/**
113 * Returns a tuple where the first element is the value of the recoil state
114 * and the second is a setter to update that state. Subscribes component
115 * to updates of the given state.
116 */
117export function useRecoilState<T>(recoilState: RecoilState<T>): [T, SetterOrUpdater<T>];
118
119/**
120 * Returns a tuple where the first element is a Loadable and the second
121 * element is a setter function to update the given state. Subscribes
122 * component to updates of the given state.
123 */
124export function useRecoilStateLoadable<T>(recoilState: RecoilState<T>): [Loadable<T>, SetterOrUpdater<T>];
125
126/**
127 * Returns a setter function for updating Recoil state. Does not subscribe
128 * the component to the given state.
129 */
130
131export function useSetRecoilState<T>(recoilState: RecoilState<T>): SetterOrUpdater<T>;
132
133/**
134 * Returns a function that will reset the given state to its default value.
135 */
136export function useResetRecoilState(recoilState: RecoilState<any>): Resetter;
137
138/**
139 * Returns a function that will run the callback that was passed when
140 * calling this hook. Useful for accessing Recoil state in response to
141 * events.
142 */
143export function useRecoilCallback<Args extends ReadonlyArray<unknown>, Return>(
144 fn: (interface: CallbackInterface) => (...args: Args) => Return,
145 deps?: ReadonlyArray<unknown>,
146): (...args: Args) => Return;
147
148export function useRecoilTransactionObserver_UNSTABLE(
149 callback: (opts: {
150 snapshot: Snapshot,
151 previousSnapshot: Snapshot,
152 }) => void,
153): void;
154
155export function useGotoRecoilSnapshot(): (snapshot: Snapshot) => void;
156
157export function useRecoilSnapshot(): Snapshot;
158
159// loadable.d.ts
160type ResolvedLoadablePromiseInfo<T> = Readonly<{
161 value: T;
162}>;
163
164export type LoadablePromise<T> = Promise<ResolvedLoadablePromiseInfo<T>>;
165
166export type Loadable<T> =
167 | Readonly<{
168 state: 'hasValue';
169 contents: T;
170 }>
171 | Readonly<{
172 state: 'hasError';
173 contents: Error;
174 }>
175 | Readonly<{
176 state: 'loading';
177 contents: LoadablePromise<T>;
178 }>;
179
180// recoilValue.d.ts
181declare class AbstractRecoilValue<T> {
182 __tag: [T];
183 __cTag: (t: T) => void; // for contravariance
184
185 key: NodeKey;
186 constructor(newKey: NodeKey);
187}
188
189declare class AbstractRecoilValueReadonly<T> {
190 __tag: [T];
191
192 key: NodeKey;
193 constructor(newKey: NodeKey);
194}
195
196export class RecoilState<T> extends AbstractRecoilValue<T> {}
197export class RecoilValueReadOnly<T> extends AbstractRecoilValueReadonly<T> {}
198export type RecoilValue<T> = RecoilValueReadOnly<T> | RecoilState<T>;
199
200export function isRecoilValue(val: unknown): val is RecoilValue<any>;
201
202/** Utilities */
203
204// bigint not supported yet
205type Primitive = undefined | null | boolean | number | symbol | string;
206
207export type SerializableParam = Primitive | SerializableParam[] | { [key: string]: SerializableParam };
208
209export interface AtomFamilyOptions<T, P extends SerializableParam> {
210 key: NodeKey;
211 dangerouslyAllowMutability?: boolean;
212 default: RecoilValue<T> | Promise<T> | T | ((param: P) => T | RecoilValue<T> | Promise<T>);
213}
214
215export function atomFamily<T, P extends SerializableParam>(
216 options: AtomFamilyOptions<T, P>,
217): (param: P) => RecoilState<T>;
218
219export interface ReadOnlySelectorFamilyOptions<T, P extends SerializableParam> {
220 key: string;
221 get: (param: P) => (opts: { get: GetRecoilValue }) => Promise<T> | RecoilValue<T> | T;
222 // cacheImplementation_UNSTABLE?: () => CacheImplementation<Loadable<T>>,
223 // cacheImplementationForParams_UNSTABLE?: () => CacheImplementation<
224 // RecoilValue<T>,
225 // >,
226 dangerouslyAllowMutability?: boolean;
227}
228
229export interface ReadWriteSelectorFamilyOptions<T, P extends SerializableParam> {
230 key: string;
231 get: (param: P) => (opts: { get: GetRecoilValue }) => Promise<T> | RecoilValue<T> | T;
232 set: (
233 param: P,
234 ) => (
235 opts: { set: SetRecoilState; get: GetRecoilValue; reset: ResetRecoilState },
236 newValue: T | DefaultValue,
237 ) => void;
238 // cacheImplementation_UNSTABLE?: () => CacheImplementation<Loadable<T>>,
239 // cacheImplementationForParams_UNSTABLE?: () => CacheImplementation<
240 // RecoilValue<T>,
241 // >,
242 dangerouslyAllowMutability?: boolean;
243}
244
245export function selectorFamily<T, P extends SerializableParam>(
246 options: ReadWriteSelectorFamilyOptions<T, P>,
247): (param: P) => RecoilState<T>;
248
249export function selectorFamily<T, P extends SerializableParam>(
250 options: ReadOnlySelectorFamilyOptions<T, P>,
251): (param: P) => RecoilValueReadOnly<T>;
252
253export function constSelector<T extends SerializableParam>(constant: T): RecoilValueReadOnly<T>;
254
255export function errorSelector(message: string): RecoilValueReadOnly<never>;
256
257export function readOnlySelector<T>(atom: RecoilValue<T>): RecoilValueReadOnly<T>;
258
259export function noWait<T>(state: RecoilValue<T>): RecoilValueReadOnly<Loadable<T>>;
260
261export type UnwrapRecoilValue<T> = T extends RecoilValue<infer R> ? R : never;
262
263export type UnwrapRecoilValueLoadables<T extends Array<RecoilValue<any>> | { [key: string]: RecoilValue<any> }> = {
264 [P in keyof T]: Loadable<UnwrapRecoilValue<T[P]>>;
265};
266
267export function waitForNone<RecoilValues extends Array<RecoilValue<any>> | [RecoilValue<any>]>(
268 param: RecoilValues,
269): RecoilValueReadOnly<UnwrapRecoilValueLoadables<RecoilValues>>;
270
271export function waitForNone<RecoilValues extends { [key: string]: RecoilValue<any> }>(
272 param: RecoilValues,
273): RecoilValueReadOnly<UnwrapRecoilValueLoadables<RecoilValues>>;
274
275export function waitForAny<RecoilValues extends Array<RecoilValue<any>> | [RecoilValue<any>]>(
276 param: RecoilValues,
277): RecoilValueReadOnly<UnwrapRecoilValueLoadables<RecoilValues>>;
278
279export function waitForAny<RecoilValues extends { [key: string]: RecoilValue<any> }>(
280 param: RecoilValues,
281): RecoilValueReadOnly<UnwrapRecoilValueLoadables<RecoilValues>>;
282
283export function waitForAll<RecoilValues extends Array<RecoilValue<any>> | [RecoilValue<any>]>(
284 param: RecoilValues,
285): RecoilValueReadOnly<UnwrapRecoilValueLoadables<RecoilValues>>;
286
287export function waitForAll<RecoilValues extends { [key: string]: RecoilValue<any> }>(
288 param: RecoilValues,
289): RecoilValueReadOnly<UnwrapRecoilValueLoadables<RecoilValues>>;