// Minimum TypeScript Version: 3.7 /** * 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. */ export { }; import * as React from 'react'; // state.d.ts type NodeKey = string; // node.d.ts export class DefaultValue { private __tag: 'DefaultValue'; } // recoilRoot.d.ts export type RecoilRootProps = { initializeState?: (mutableSnapshot: MutableSnapshot) => void, override?: true, } | {override: false}; export const RecoilRoot: React.FC; // Snapshot.d.ts declare const SnapshotID_OPAQUE: unique symbol; export interface SnapshotID { readonly [SnapshotID_OPAQUE]: true; } interface ComponentInfo { name: string; } interface AtomInfo { loadable?: Loadable; isActive: boolean; isSet: boolean; isModified: boolean; // TODO report modified selectors type: 'atom' | 'selector' | undefined; // undefined until initialized for now deps: Iterable>; subscribers: { nodes: Iterable>, components: Iterable, }; } export class Snapshot { getID(): SnapshotID; getLoadable(recoilValue: RecoilValue): Loadable; getPromise(recoilValue: RecoilValue): Promise; getNodes_UNSTABLE(opts?: { isModified?: boolean, isInitialized?: boolean }): Iterable>; getInfo_UNSTABLE(recoilValue: RecoilValue): AtomInfo; map(cb: (mutableSnapshot: MutableSnapshot) => void): Snapshot; asyncMap(cb: (mutableSnapshot: MutableSnapshot) => Promise): Promise; retain(): () => void; } export class MutableSnapshot extends Snapshot { set: SetRecoilState; reset: ResetRecoilState; } // Effect is called the first time a node is used with a export type AtomEffect = (param: { node: RecoilState, trigger: 'set' | 'get', // Call synchronously to initialize value or async to change it later setSelf: (param: | T | DefaultValue | Promise | ((param: T | DefaultValue) => T | DefaultValue), ) => void, resetSelf: () => void, // Subscribe callbacks to events. // Atom effect observers are called before global transaction observers onSet: ( param: (newValue: T, oldValue: T | DefaultValue) => void, ) => void, }) => void | (() => void); // atom.d.ts export interface AtomOptions { key: NodeKey; default: RecoilValue | Promise | T; effects_UNSTABLE?: ReadonlyArray>; dangerouslyAllowMutability?: boolean; } /** * Creates an atom, which represents a piece of writeable state */ export function atom(options: AtomOptions): RecoilState; // selector.d.ts export type GetRecoilValue = (recoilVal: RecoilValue) => T; export type GetCallback = , Return>( fn: (interface: Readonly<{snapshot: Snapshot}>) => (...args: Args) => Return, ) => (...args: Args) => Return; export type SetRecoilState = ( recoilVal: RecoilState, newVal: T | DefaultValue | ((prevValue: T) => T | DefaultValue), ) => void; export type ResetRecoilState = (recoilVal: RecoilState) => void; // eslint-disable-line @typescript-eslint/no-explicit-any // export type EqualityPolicy = 'reference' | 'value'; TODO: removing while we discuss long term API export type EvictionPolicy = 'lru' | 'keep-all' | 'most-recent'; // TODO: removing while we discuss long term API // export type CachePolicy = // | {eviction: 'lru', maxSize: number, equality?: EqualityPolicy} // | {eviction: 'none', equality?: EqualityPolicy} // | {eviction?: undefined, equality: EqualityPolicy}; // TODO: removing while we discuss long term API // export interface CachePolicyWithoutEviction { // equality: EqualityPolicy; // } export type CachePolicyWithoutEquality = {eviction: 'lru', maxSize: number} | {eviction: 'keep-all'} | {eviction: 'most-recent'}; export interface ReadOnlySelectorOptions { key: string; get: (opts: { get: GetRecoilValue, getCallback: GetCallback, }) => Promise | RecoilValue | T; dangerouslyAllowMutability?: boolean; cachePolicy_UNSTABLE?: CachePolicyWithoutEquality; // TODO: using the more restrictive CachePolicyWithoutEquality while we discuss long term API } export interface ReadWriteSelectorOptions extends ReadOnlySelectorOptions { set: ( opts: { set: SetRecoilState; get: GetRecoilValue; reset: ResetRecoilState; }, newValue: T | DefaultValue, ) => void; } export function selector(options: ReadWriteSelectorOptions): RecoilState; export function selector(options: ReadOnlySelectorOptions): RecoilValueReadOnly; // hooks.d.ts export type SetterOrUpdater = (valOrUpdater: ((currVal: T) => T) | T) => void; export type Resetter = () => void; export interface TransactionInterface_UNSTABLE { get(a: RecoilValue): T; set(s: RecoilState, u: ((currVal: T) => T) | T): void; reset(s: RecoilState): void; } export type CallbackInterface = Readonly<{ set: (recoilVal: RecoilState, valOrUpdater: ((currVal: T) => T) | T) => void; reset: (recoilVal: RecoilState) => void; // eslint-disable-line @typescript-eslint/no-explicit-any snapshot: Snapshot, gotoSnapshot: (snapshot: Snapshot) => void, transact_UNSTABLE: (cb: (i: TransactionInterface_UNSTABLE) => void) => void; }>; /** * Returns the value of an atom or selector (readonly or writeable) and * subscribes the components to future updates of that state. */ export function useRecoilValue(recoilValue: RecoilValue): T; /** * Returns a Loadable representing the status of the given Recoil state * and subscribes the component to future updates of that state. Useful * for working with async selectors. */ export function useRecoilValueLoadable(recoilValue: RecoilValue): Loadable; /** * Returns a tuple where the first element is the value of the recoil state * and the second is a setter to update that state. Subscribes component * to updates of the given state. */ export function useRecoilState(recoilState: RecoilState): [T, SetterOrUpdater]; /** * Returns a tuple where the first element is a Loadable and the second * element is a setter function to update the given state. Subscribes * component to updates of the given state. */ export function useRecoilStateLoadable(recoilState: RecoilState): [Loadable, SetterOrUpdater]; /** * Returns a setter function for updating Recoil state. Does not subscribe * the component to the given state. */ export function useSetRecoilState(recoilState: RecoilState): SetterOrUpdater; /** * Returns a function that will reset the given state to its default value. */ export function useResetRecoilState(recoilState: RecoilState): Resetter; // eslint-disable-line @typescript-eslint/no-explicit-any /** * Returns current info about an atom */ export function useGetRecoilValueInfo_UNSTABLE(): (recoilValue: RecoilValue) => AtomInfo; /** * Returns a function that will run the callback that was passed when * calling this hook. Useful for accessing Recoil state in response to * events. */ export function useRecoilCallback, Return>( fn: (interface: CallbackInterface) => (...args: Args) => Return, deps?: ReadonlyArray, ): (...args: Args) => Return; export function useRecoilTransaction_UNSTABLE>( fn: (interface: TransactionInterface_UNSTABLE) => (...args: Args) => void, deps?: ReadonlyArray, ): (...args: Args) => void; export function useRecoilTransactionObserver_UNSTABLE( callback: (opts: { snapshot: Snapshot, previousSnapshot: Snapshot, }) => void, ): void; export function useGotoRecoilSnapshot(): (snapshot: Snapshot) => void; export function useRecoilSnapshot(): Snapshot; // useRecoilBridgeAcrossReactRoots.d.ts export const RecoilBridge: React.FC; export function useRecoilBridgeAcrossReactRoots_UNSTABLE(): typeof RecoilBridge; // loadable.d.ts declare const LoadablePromiseValue_OPAQUE: unique symbol; interface LoadablePromiseValue { readonly [LoadablePromiseValue_OPAQUE]: true; } type LoadablePromise = Promise; interface BaseLoadable { getValue: () => T; toPromise: () => Promise; valueOrThrow: () => T; errorOrThrow: () => any; promiseOrThrow: () => Promise; is: (other: Loadable) => boolean; map: (map: (from: T) => Promise | S) => Loadable; } interface ValueLoadable extends BaseLoadable { state: 'hasValue'; contents: T; valueMaybe: () => T; errorMaybe: () => undefined; promiseMaybe: () => undefined; } interface LoadingLoadable extends BaseLoadable { state: 'loading'; contents: LoadablePromise; valueMaybe: () => undefined; errorMaybe: () => undefined; promiseMaybe: () => Promise; } interface ErrorLoadable extends BaseLoadable { state: 'hasError'; contents: any; valueMaybe: () => undefined; errorMaybe: () => any; promiseMaybe: () => undefined; } export type Loadable = | ValueLoadable | LoadingLoadable | ErrorLoadable; // recoilValue.d.ts declare class AbstractRecoilValue { __tag: [T]; __cTag: (t: T) => void; // for contravariance key: NodeKey; constructor(newKey: NodeKey); } declare class AbstractRecoilValueReadonly { __tag: [T]; key: NodeKey; constructor(newKey: NodeKey); } export class RecoilState extends AbstractRecoilValue {} export class RecoilValueReadOnly extends AbstractRecoilValueReadonly {} export type RecoilValue = RecoilValueReadOnly | RecoilState; export function isRecoilValue(val: unknown): val is RecoilValue; // eslint-disable-line @typescript-eslint/no-explicit-any /** Utilities */ // bigint not supported yet type Primitive = undefined | null | boolean | number | symbol | string; export type SerializableParam = | Primitive | {toJSON: () => string} | ReadonlyArray | Readonly<{[key: string]: SerializableParam}>; export interface AtomFamilyOptions { key: NodeKey; dangerouslyAllowMutability?: boolean; default: RecoilValue | Promise | T | ((param: P) => T | RecoilValue | Promise); effects_UNSTABLE?: | ReadonlyArray> | ((param: P) => ReadonlyArray>); // cachePolicyForParams_UNSTABLE?: CachePolicyWithoutEviction; TODO: removing while we discuss long term API } export function atomFamily( options: AtomFamilyOptions, ): (param: P) => RecoilState; export interface ReadOnlySelectorFamilyOptions { key: string; get: (param: P) => (opts: { get: GetRecoilValue, getCallback: GetCallback, }) => Promise | RecoilValue | T; // cachePolicyForParams_UNSTABLE?: CachePolicyWithoutEviction; TODO: removing while we discuss long term API cachePolicy_UNSTABLE?: CachePolicyWithoutEquality; // TODO: using the more restrictive CachePolicyWithoutEquality while we discuss long term API dangerouslyAllowMutability?: boolean; } export interface ReadWriteSelectorFamilyOptions { key: string; get: (param: P) => (opts: { get: GetRecoilValue, getCallback: GetCallback, }) => Promise | RecoilValue | T; set: ( param: P, ) => ( opts: { set: SetRecoilState; get: GetRecoilValue; reset: ResetRecoilState }, newValue: T | DefaultValue, ) => void; // cachePolicyForParams_UNSTABLE?: CachePolicyWithoutEviction; TODO: removing while we discuss long term API cachePolicy_UNSTABLE?: CachePolicyWithoutEquality; // TODO: using the more restrictive CachePolicyWithoutEquality while we discuss long term API dangerouslyAllowMutability?: boolean; } export function selectorFamily( options: ReadWriteSelectorFamilyOptions, ): (param: P) => RecoilState; export function selectorFamily( options: ReadOnlySelectorFamilyOptions, ): (param: P) => RecoilValueReadOnly; export function constSelector(constant: T): RecoilValueReadOnly; export function errorSelector(message: string): RecoilValueReadOnly; export function readOnlySelector(atom: RecoilValue): RecoilValueReadOnly; export function noWait(state: RecoilValue): RecoilValueReadOnly>; /* eslint-disable @typescript-eslint/no-explicit-any */ export type UnwrapRecoilValue = T extends RecoilValue ? R : never; export type UnwrapRecoilValues> | { [key: string]: RecoilValue }> = { [P in keyof T]: UnwrapRecoilValue; }; export type UnwrapRecoilValueLoadables> | { [key: string]: RecoilValue }> = { [P in keyof T]: Loadable>; }; export function waitForNone> | [RecoilValue]>( param: RecoilValues, ): RecoilValueReadOnly>; export function waitForNone }>( param: RecoilValues, ): RecoilValueReadOnly>; export function waitForAny> | [RecoilValue]>( param: RecoilValues, ): RecoilValueReadOnly>; export function waitForAny }>( param: RecoilValues, ): RecoilValueReadOnly>; export function waitForAll> | [RecoilValue]>( param: RecoilValues, ): RecoilValueReadOnly>; export function waitForAll }>( param: RecoilValues, ): RecoilValueReadOnly>; export function waitForAllSettled> | [RecoilValue]>( param: RecoilValues, ): RecoilValueReadOnly>; export function waitForAllSettled }>( param: RecoilValues, ): RecoilValueReadOnly>; /* eslint-enable @typescript-eslint/no-explicit-any */ export function snapshot_UNSTABLE(initializeState?: (shapshot: MutableSnapshot) => void): Snapshot;