reselect.d.ts 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  1. /**
  2. * Represents the longest array within an array of arrays.
  3. *
  4. * @template ArrayOfTuples An array of arrays.
  5. *
  6. * @internal
  7. */
  8. type LongestTuple<ArrayOfTuples extends readonly unknown[][]> = ArrayOfTuples extends [infer FirstArray extends unknown[]] ? FirstArray : ArrayOfTuples extends [
  9. infer FirstArray,
  10. ...infer RestArrays extends unknown[][]
  11. ] ? LongerOfTwo<FirstArray, LongestTuple<RestArrays>> : never;
  12. /**
  13. * Determines the longer of two array types.
  14. *
  15. * @template ArrayOne First array type.
  16. * @template ArrayTwo Second array type.
  17. *
  18. * @internal
  19. */
  20. type LongerOfTwo<ArrayOne, ArrayTwo> = keyof ArrayTwo extends keyof ArrayOne ? ArrayOne : ArrayTwo;
  21. /**
  22. * Extracts the element at a specific index in an array.
  23. *
  24. * @template ArrayType The array type.
  25. * @template Index The index type.
  26. *
  27. * @internal
  28. */
  29. type ElementAt<ArrayType extends unknown[], Index extends PropertyKey> = Index extends keyof ArrayType ? ArrayType[Index] : unknown;
  30. /**
  31. * Maps each array in an array of arrays to its element at a given index.
  32. *
  33. * @template ArrayOfTuples An array of arrays.
  34. * @template Index The index to extract from each array.
  35. *
  36. * @internal
  37. */
  38. type ElementsAtGivenIndex<ArrayOfTuples extends readonly unknown[][], Index extends PropertyKey> = {
  39. [ArrayIndex in keyof ArrayOfTuples]: ElementAt<ArrayOfTuples[ArrayIndex], Index>;
  40. };
  41. /**
  42. * Computes the intersection of all types in a tuple.
  43. *
  44. * @template Tuple A tuple of types.
  45. *
  46. * @internal
  47. */
  48. type Intersect<Tuple extends readonly unknown[]> = Tuple extends [] ? unknown : Tuple extends [infer Head, ...infer Tail] ? Head & Intersect<Tail> : Tuple[number];
  49. /**
  50. * Merges a tuple of arrays into a single tuple, intersecting types at each index.
  51. *
  52. * @template ArrayOfTuples An array of tuples.
  53. * @template LongestArray The longest array in ArrayOfTuples.
  54. *
  55. * @internal
  56. */
  57. type MergeTuples<ArrayOfTuples extends readonly unknown[][], LongestArray extends unknown[] = LongestTuple<ArrayOfTuples>> = {
  58. [Index in keyof LongestArray]: Intersect<ElementsAtGivenIndex<ArrayOfTuples, Index>>;
  59. };
  60. /**
  61. * Extracts the parameter types from a tuple of functions.
  62. *
  63. * @template FunctionsArray An array of function types.
  64. *
  65. * @internal
  66. */
  67. type ExtractParameters<FunctionsArray extends readonly AnyFunction[]> = {
  68. [Index in keyof FunctionsArray]: Parameters<FunctionsArray[Index]>;
  69. };
  70. /**
  71. * Merges the parameters of a tuple of functions into a single tuple.
  72. *
  73. * @template FunctionsArray An array of function types.
  74. *
  75. * @internal
  76. */
  77. type MergeParameters<FunctionsArray extends readonly AnyFunction[]> = '0' extends keyof FunctionsArray ? MergeTuples<ExtractParameters<FunctionsArray>> : Parameters<FunctionsArray[number]>;
  78. /**
  79. * Configuration options for a memoization function utilizing `WeakMap` for
  80. * its caching mechanism.
  81. *
  82. * @template Result - The type of the return value of the memoized function.
  83. *
  84. * @since 5.0.0
  85. * @public
  86. */
  87. interface WeakMapMemoizeOptions<Result = any> {
  88. /**
  89. * If provided, used to compare a newly generated output value against previous values in the cache.
  90. * If a match is found, the old value is returned. This addresses the common
  91. * ```ts
  92. * todos.map(todo => todo.id)
  93. * ```
  94. * use case, where an update to another field in the original data causes a recalculation
  95. * due to changed references, but the output is still effectively the same.
  96. *
  97. * @since 5.0.0
  98. */
  99. resultEqualityCheck?: EqualityFn<Result>;
  100. }
  101. /**
  102. * Creates a tree of `WeakMap`-based cache nodes based on the identity of the
  103. * arguments it's been called with (in this case, the extracted values from your input selectors).
  104. * This allows `weakMapMemoize` to have an effectively infinite cache size.
  105. * Cache results will be kept in memory as long as references to the arguments still exist,
  106. * and then cleared out as the arguments are garbage-collected.
  107. *
  108. * __Design Tradeoffs for `weakMapMemoize`:__
  109. * - Pros:
  110. * - It has an effectively infinite cache size, but you have no control over
  111. * how long values are kept in cache as it's based on garbage collection and `WeakMap`s.
  112. * - Cons:
  113. * - There's currently no way to alter the argument comparisons.
  114. * They're based on strict reference equality.
  115. * - It's roughly the same speed as `lruMemoize`, although likely a fraction slower.
  116. *
  117. * __Use Cases for `weakMapMemoize`:__
  118. * - This memoizer is likely best used for cases where you need to call the
  119. * same selector instance with many different arguments, such as a single
  120. * selector instance that is used in a list item component and called with
  121. * item IDs like:
  122. * ```ts
  123. * useSelector(state => selectSomeData(state, props.category))
  124. * ```
  125. * @param func - The function to be memoized.
  126. * @returns A memoized function with a `.clearCache()` method attached.
  127. *
  128. * @example
  129. * <caption>Using `createSelector`</caption>
  130. * ```ts
  131. * import { createSelector, weakMapMemoize } from 'reselect'
  132. *
  133. * interface RootState {
  134. * items: { id: number; category: string; name: string }[]
  135. * }
  136. *
  137. * const selectItemsByCategory = createSelector(
  138. * [
  139. * (state: RootState) => state.items,
  140. * (state: RootState, category: string) => category
  141. * ],
  142. * (items, category) => items.filter(item => item.category === category),
  143. * {
  144. * memoize: weakMapMemoize,
  145. * argsMemoize: weakMapMemoize
  146. * }
  147. * )
  148. * ```
  149. *
  150. * @example
  151. * <caption>Using `createSelectorCreator`</caption>
  152. * ```ts
  153. * import { createSelectorCreator, weakMapMemoize } from 'reselect'
  154. *
  155. * const createSelectorWeakMap = createSelectorCreator({ memoize: weakMapMemoize, argsMemoize: weakMapMemoize })
  156. *
  157. * const selectItemsByCategory = createSelectorWeakMap(
  158. * [
  159. * (state: RootState) => state.items,
  160. * (state: RootState, category: string) => category
  161. * ],
  162. * (items, category) => items.filter(item => item.category === category)
  163. * )
  164. * ```
  165. *
  166. * @template Func - The type of the function that is memoized.
  167. *
  168. * @see {@link https://reselect.js.org/api/weakMapMemoize `weakMapMemoize`}
  169. *
  170. * @since 5.0.0
  171. * @public
  172. * @experimental
  173. */
  174. declare function weakMapMemoize<Func extends AnyFunction>(func: Func, options?: WeakMapMemoizeOptions<ReturnType<Func>>): Func & {
  175. clearCache: () => void;
  176. resultsCount: () => number;
  177. resetResultsCount: () => void;
  178. };
  179. /**
  180. * A standard selector function.
  181. * @template State - The first value, often a Redux root state object.
  182. * @template Result - The final result returned by the selector.
  183. * @template Params - All additional arguments passed into the selector.
  184. *
  185. * @public
  186. */
  187. type Selector<State = any, Result = unknown, Params extends readonly any[] = any[]> = Distribute<
  188. /**
  189. * A function that takes a state and returns data that is based on that state.
  190. *
  191. * @param state - The first argument, often a Redux root state object.
  192. * @param params - All additional arguments passed into the selector.
  193. * @returns A derived value from the state.
  194. */
  195. (state: State, ...params: FallbackIfNever<Params, []>) => Result>;
  196. /**
  197. * An array of input selectors.
  198. *
  199. * @public
  200. */
  201. type SelectorArray = readonly Selector[];
  202. /**
  203. * Extracts an array of all return types from all input selectors.
  204. *
  205. * @public
  206. */
  207. type SelectorResultArray<Selectors extends SelectorArray> = ExtractReturnType<Selectors>;
  208. /**
  209. * The options object used inside `createSelector` and `createSelectorCreator`.
  210. *
  211. * @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`).
  212. * @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used.
  213. * @template OverrideMemoizeFunction - The type of the optional `memoize` function that could be passed into the options object inside `createSelector` to override the original `memoize` function that was initially passed into `createSelectorCreator`.
  214. * @template OverrideArgsMemoizeFunction - The type of the optional `argsMemoize` function that could be passed into the options object inside `createSelector` to override the original `argsMemoize` function that was initially passed into `createSelectorCreator`. If none was initially provided, `weakMapMemoize` will be used.
  215. *
  216. * @public
  217. */
  218. interface CreateSelectorOptions<MemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize, ArgsMemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize, OverrideMemoizeFunction extends UnknownMemoizer = never, OverrideArgsMemoizeFunction extends UnknownMemoizer = never> {
  219. /**
  220. * Reselect performs additional checks in development mode to help identify
  221. * and warn about potential issues in selector behavior. This option
  222. * allows you to customize the behavior of these checks per selector.
  223. *
  224. * @see {@link https://reselect.js.org/api/development-only-stability-checks Development-Only Stability Checks}
  225. *
  226. * @since 5.0.0
  227. */
  228. devModeChecks?: Partial<DevModeChecks>;
  229. /**
  230. * The memoize function that is used to memoize the {@linkcode OutputSelectorFields.resultFunc resultFunc}
  231. * inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`).
  232. *
  233. * When passed directly into `createSelector`, it overrides the `memoize` function initially passed into `createSelectorCreator`.
  234. *
  235. * @example
  236. * ```ts
  237. * import { createSelector, weakMapMemoize } from 'reselect'
  238. *
  239. * const selectItemsByCategory = createSelector(
  240. * [
  241. * (state: RootState) => state.items,
  242. * (state: RootState, category: string) => category
  243. * ],
  244. * (items, category) => items.filter(item => item.category === category),
  245. * { memoize: weakMapMemoize }
  246. * )
  247. * ```
  248. *
  249. * @since 5.0.0
  250. */
  251. memoize?: FallbackIfNever<OverrideMemoizeFunction, MemoizeFunction>;
  252. /**
  253. * The optional memoize function that is used to memoize the arguments
  254. * passed into the output selector generated by `createSelector`
  255. * (e.g., `lruMemoize` or `weakMapMemoize`).
  256. *
  257. * When passed directly into `createSelector`, it overrides the
  258. * `argsMemoize` function initially passed into `createSelectorCreator`.
  259. * If none was initially provided, `weakMapMemoize` will be used.
  260. *
  261. * @example
  262. * ```ts
  263. * import { createSelector, weakMapMemoize } from 'reselect'
  264. *
  265. * const selectItemsByCategory = createSelector(
  266. * [
  267. * (state: RootState) => state.items,
  268. * (state: RootState, category: string) => category
  269. * ],
  270. * (items, category) => items.filter(item => item.category === category),
  271. * { argsMemoize: weakMapMemoize }
  272. * )
  273. * ```
  274. *
  275. * @default weakMapMemoize
  276. *
  277. * @since 5.0.0
  278. */
  279. argsMemoize?: FallbackIfNever<OverrideArgsMemoizeFunction, ArgsMemoizeFunction>;
  280. /**
  281. * Optional configuration options for the {@linkcode CreateSelectorOptions.memoize memoize} function.
  282. * These options are passed to the {@linkcode CreateSelectorOptions.memoize memoize} function as the second argument.
  283. *
  284. * @since 5.0.0
  285. */
  286. memoizeOptions?: OverrideMemoizeOptions<MemoizeFunction, OverrideMemoizeFunction>;
  287. /**
  288. * Optional configuration options for the {@linkcode CreateSelectorOptions.argsMemoize argsMemoize} function.
  289. * These options are passed to the {@linkcode CreateSelectorOptions.argsMemoize argsMemoize} function as the second argument.
  290. *
  291. * @since 5.0.0
  292. */
  293. argsMemoizeOptions?: OverrideMemoizeOptions<ArgsMemoizeFunction, OverrideArgsMemoizeFunction>;
  294. }
  295. /**
  296. * The additional fields attached to the output selector generated by `createSelector`.
  297. *
  298. * **Note**: Although {@linkcode CreateSelectorOptions.memoize memoize}
  299. * and {@linkcode CreateSelectorOptions.argsMemoize argsMemoize} are included in the attached fields,
  300. * the fields themselves are independent of the type of
  301. * {@linkcode CreateSelectorOptions.memoize memoize} and {@linkcode CreateSelectorOptions.argsMemoize argsMemoize} functions.
  302. * Meaning this type is not going to generate additional fields based on what functions we use to memoize our selectors.
  303. *
  304. * _This type is not to be confused with {@linkcode ExtractMemoizerFields ExtractMemoizerFields}._
  305. *
  306. * @template InputSelectors - The type of the input selectors.
  307. * @template Result - The type of the result returned by the `resultFunc`.
  308. * @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`).
  309. * @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used.
  310. *
  311. * @public
  312. */
  313. type OutputSelectorFields<InputSelectors extends SelectorArray = SelectorArray, Result = unknown, MemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize, ArgsMemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize> = {
  314. /**
  315. * The final function passed to `createSelector`. Otherwise known as the `combiner`.
  316. */
  317. resultFunc: Combiner<InputSelectors, Result>;
  318. /**
  319. * The memoized version of {@linkcode OutputSelectorFields.resultFunc resultFunc}.
  320. */
  321. memoizedResultFunc: Combiner<InputSelectors, Result> & ExtractMemoizerFields<MemoizeFunction>;
  322. /**
  323. * @Returns The last result calculated by {@linkcode OutputSelectorFields.memoizedResultFunc memoizedResultFunc}.
  324. */
  325. lastResult: () => Result;
  326. /**
  327. * The array of the input selectors used by `createSelector` to compose the
  328. * combiner ({@linkcode OutputSelectorFields.memoizedResultFunc memoizedResultFunc}).
  329. */
  330. dependencies: InputSelectors;
  331. /**
  332. * Counts the number of times {@linkcode OutputSelectorFields.memoizedResultFunc memoizedResultFunc} has been recalculated.
  333. */
  334. recomputations: () => number;
  335. /**
  336. * Resets the count of {@linkcode OutputSelectorFields.recomputations recomputations} count to 0.
  337. */
  338. resetRecomputations: () => void;
  339. /**
  340. * Counts the number of times the input selectors ({@linkcode OutputSelectorFields.dependencies dependencies})
  341. * have been recalculated. This is distinct from {@linkcode OutputSelectorFields.recomputations recomputations},
  342. * which tracks the recalculations of the result function.
  343. *
  344. * @since 5.0.0
  345. */
  346. dependencyRecomputations: () => number;
  347. /**
  348. * Resets the count {@linkcode OutputSelectorFields.dependencyRecomputations dependencyRecomputations}
  349. * for the input selectors ({@linkcode OutputSelectorFields.dependencies dependencies})
  350. * of a memoized selector.
  351. *
  352. * @since 5.0.0
  353. */
  354. resetDependencyRecomputations: () => void;
  355. } & Simplify<Required<Pick<CreateSelectorOptions<MemoizeFunction, ArgsMemoizeFunction>, 'argsMemoize' | 'memoize'>>>;
  356. /**
  357. * Represents the actual selectors generated by `createSelector`.
  358. *
  359. * @template InputSelectors - The type of the input selectors.
  360. * @template Result - The type of the result returned by the `resultFunc`.
  361. * @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`).
  362. * @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used.
  363. *
  364. * @public
  365. */
  366. type OutputSelector<InputSelectors extends SelectorArray = SelectorArray, Result = unknown, MemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize, ArgsMemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize> = Selector<GetStateFromSelectors<InputSelectors>, Result, GetParamsFromSelectors<InputSelectors>> & ExtractMemoizerFields<ArgsMemoizeFunction> & OutputSelectorFields<InputSelectors, Result, MemoizeFunction, ArgsMemoizeFunction>;
  367. /**
  368. * A function that takes input selectors' return values as arguments and returns a result. Otherwise known as `resultFunc`.
  369. *
  370. * @template InputSelectors - An array of input selectors.
  371. * @template Result - Result returned by `resultFunc`.
  372. *
  373. * @public
  374. */
  375. type Combiner<InputSelectors extends SelectorArray, Result> = Distribute<
  376. /**
  377. * A function that takes input selectors' return values as arguments and returns a result. Otherwise known as `resultFunc`.
  378. *
  379. * @param resultFuncArgs - Return values of input selectors.
  380. * @returns The return value of {@linkcode OutputSelectorFields.resultFunc resultFunc}.
  381. */
  382. (...resultFuncArgs: SelectorResultArray<InputSelectors>) => Result>;
  383. /**
  384. * A standard function returning true if two values are considered equal.
  385. *
  386. * @public
  387. */
  388. type EqualityFn<T = any> = (a: T, b: T) => boolean;
  389. /**
  390. * The frequency of development mode checks.
  391. *
  392. * @since 5.0.0
  393. * @public
  394. */
  395. type DevModeCheckFrequency = 'always' | 'once' | 'never';
  396. /**
  397. * Represents the configuration for development mode checks.
  398. *
  399. * @since 5.0.0
  400. * @public
  401. */
  402. interface DevModeChecks {
  403. /**
  404. * Overrides the global input stability check for the selector.
  405. * - `once` - Run only the first time the selector is called.
  406. * - `always` - Run every time the selector is called.
  407. * - `never` - Never run the input stability check.
  408. *
  409. * @default 'once'
  410. *
  411. * @see {@link https://reselect.js.org/api/development-only-stability-checks Development-Only Stability Checks}
  412. * @see {@link https://reselect.js.org/api/development-only-stability-checks#inputstabilitycheck `inputStabilityCheck`}
  413. * @see {@link https://reselect.js.org/api/development-only-stability-checks#2-per-selector-by-passing-an-inputstabilitycheck-option-directly-to- per-selector-configuration}
  414. *
  415. * @since 5.0.0
  416. */
  417. inputStabilityCheck: DevModeCheckFrequency;
  418. /**
  419. * Overrides the global identity function check for the selector.
  420. * - `once` - Run only the first time the selector is called.
  421. * - `always` - Run every time the selector is called.
  422. * - `never` - Never run the identity function check.
  423. *
  424. * @default 'once'
  425. *
  426. * @see {@link https://reselect.js.org/api/development-only-stability-checks Development-Only Stability Checks}
  427. * @see {@link https://reselect.js.org/api/development-only-stability-checks#identityfunctioncheck `identityFunctionCheck`}
  428. * @see {@link https://reselect.js.org/api/development-only-stability-checks#2-per-selector-by-passing-an-identityfunctioncheck-option-directly-to- per-selector-configuration}
  429. *
  430. * @since 5.0.0
  431. */
  432. identityFunctionCheck: DevModeCheckFrequency;
  433. }
  434. /**
  435. * Represents execution information for development mode checks.
  436. *
  437. * @public
  438. * @since 5.0.0
  439. */
  440. type DevModeChecksExecutionInfo = {
  441. [K in keyof DevModeChecks]: {
  442. /**
  443. * A boolean indicating whether the check should be executed.
  444. */
  445. shouldRun: boolean;
  446. /**
  447. * The function to execute for the check.
  448. */
  449. run: AnyFunction;
  450. };
  451. };
  452. /**
  453. * Determines the combined single "State" type (first arg) from all input selectors.
  454. *
  455. * @public
  456. */
  457. type GetStateFromSelectors<Selectors extends SelectorArray> = MergeParameters<Selectors>[0];
  458. /**
  459. * Determines the combined "Params" type (all remaining args) from all input selectors.
  460. *
  461. * @public
  462. */
  463. type GetParamsFromSelectors<Selectors extends SelectorArray> = ArrayTail<MergeParameters<Selectors>>;
  464. /**
  465. * Any Memoizer function. A memoizer is a function that accepts another function and returns it.
  466. *
  467. * @template FunctionType - The type of the function that is memoized.
  468. *
  469. * @public
  470. */
  471. type UnknownMemoizer<FunctionType extends UnknownFunction = UnknownFunction> = (func: FunctionType, ...options: any[]) => FunctionType;
  472. /**
  473. * Extracts the options type for a memoization function based on its parameters.
  474. * The first parameter of the function is expected to be the function to be memoized,
  475. * followed by options for the memoization process.
  476. *
  477. * @template MemoizeFunction - The type of the memoize function to be checked.
  478. *
  479. * @public
  480. */
  481. type MemoizeOptionsFromParameters<MemoizeFunction extends UnknownMemoizer> = (NonFunctionType<DropFirstParameter<MemoizeFunction>[0]> | FunctionType<DropFirstParameter<MemoizeFunction>[0]>) | (NonFunctionType<DropFirstParameter<MemoizeFunction>[number]> | FunctionType<DropFirstParameter<MemoizeFunction>[number]>)[];
  482. /**
  483. * Derive the type of memoize options object based on whether the memoize function itself was overridden.
  484. *
  485. * _This type can be used for both `memoizeOptions` and `argsMemoizeOptions`._
  486. *
  487. * @template MemoizeFunction - The type of the `memoize` or `argsMemoize` function initially passed into `createSelectorCreator`.
  488. * @template OverrideMemoizeFunction - The type of the optional `memoize` or `argsMemoize` function passed directly into `createSelector` which then overrides the original `memoize` or `argsMemoize` function passed into `createSelectorCreator`.
  489. *
  490. * @public
  491. */
  492. type OverrideMemoizeOptions<MemoizeFunction extends UnknownMemoizer, OverrideMemoizeFunction extends UnknownMemoizer = never> = IfNever<OverrideMemoizeFunction, Simplify<MemoizeOptionsFromParameters<MemoizeFunction>>, Simplify<MemoizeOptionsFromParameters<OverrideMemoizeFunction>>>;
  493. /**
  494. * Extracts the additional properties or methods that a memoize function attaches to
  495. * the function it memoizes (e.g., `clearCache`).
  496. *
  497. * @template MemoizeFunction - The type of the memoize function to be checked.
  498. *
  499. * @public
  500. */
  501. type ExtractMemoizerFields<MemoizeFunction extends UnknownMemoizer> = Simplify<OmitIndexSignature<ReturnType<MemoizeFunction>>>;
  502. /**
  503. * Represents the additional properties attached to a function memoized by `reselect`.
  504. *
  505. * `lruMemoize`, `weakMapMemoize` and `autotrackMemoize` all return these properties.
  506. *
  507. * @see {@linkcode ExtractMemoizerFields ExtractMemoizerFields}
  508. *
  509. * @public
  510. */
  511. type DefaultMemoizeFields = {
  512. /**
  513. * Clears the memoization cache associated with a memoized function.
  514. * This method is typically used to reset the state of the cache, allowing
  515. * for the garbage collection of previously memoized results and ensuring
  516. * that future calls to the function recompute the results.
  517. */
  518. clearCache: () => void;
  519. resultsCount: () => number;
  520. resetResultsCount: () => void;
  521. };
  522. /**
  523. * Any function with any arguments.
  524. *
  525. * @internal
  526. */
  527. type AnyFunction = (...args: any[]) => any;
  528. /**
  529. * Any function with unknown arguments.
  530. *
  531. * @internal
  532. */
  533. type UnknownFunction = (...args: unknown[]) => unknown;
  534. /**
  535. * When a generic type parameter is using its default value of `never`, fallback to a different type.
  536. *
  537. * @template T - Type to be checked.
  538. * @template FallbackTo - Type to fallback to if `T` resolves to `never`.
  539. *
  540. * @internal
  541. */
  542. type FallbackIfNever<T, FallbackTo> = IfNever<T, FallbackTo, T>;
  543. /**
  544. * Extracts the non-function part of a type.
  545. *
  546. * @template T - The input type to be refined by excluding function types and index signatures.
  547. *
  548. * @internal
  549. */
  550. type NonFunctionType<T> = Simplify<OmitIndexSignature<Exclude<T, AnyFunction>>>;
  551. /**
  552. * Extracts the function part of a type.
  553. *
  554. * @template T - The input type to be refined by extracting function types.
  555. *
  556. * @internal
  557. */
  558. type FunctionType<T> = Extract<T, AnyFunction>;
  559. /**
  560. * Extracts the return type from all functions as a tuple.
  561. *
  562. * @internal
  563. */
  564. type ExtractReturnType<FunctionsArray extends readonly AnyFunction[]> = {
  565. [Index in keyof FunctionsArray]: FunctionsArray[Index] extends FunctionsArray[number] ? FallbackIfUnknown<ReturnType<FunctionsArray[Index]>, any> : never;
  566. };
  567. /**
  568. * Utility type to infer the type of "all params of a function except the first",
  569. * so we can determine what arguments a memoize function accepts.
  570. *
  571. * @internal
  572. */
  573. type DropFirstParameter<Func extends AnyFunction> = Func extends (firstArg: any, ...restArgs: infer Rest) => any ? Rest : never;
  574. /**
  575. * Distributes over a type. It is used mostly to expand a function type
  576. * in hover previews while preserving their original JSDoc information.
  577. *
  578. * If preserving JSDoc information is not a concern, you can use {@linkcode ExpandFunction ExpandFunction}.
  579. *
  580. * @template T The type to be distributed.
  581. *
  582. * @internal
  583. */
  584. type Distribute<T> = T extends T ? T : never;
  585. /**
  586. * Extracts the type of an array or tuple minus the first element.
  587. *
  588. * @internal
  589. */
  590. type ArrayTail<ArrayType> = ArrayType extends readonly [
  591. unknown,
  592. ...infer Tail
  593. ] ? Tail : [];
  594. /**
  595. * An alias for type `{}`. Represents any value that is not `null` or `undefined`.
  596. * It is mostly used for semantic purposes to help distinguish between an
  597. * empty object type and `{}` as they are not the same.
  598. *
  599. * @internal
  600. */
  601. type AnyNonNullishValue = NonNullable<unknown>;
  602. /**
  603. * Same as {@linkcode AnyNonNullishValue AnyNonNullishValue} but aliased
  604. * for semantic purposes. It is intended to be used in scenarios where
  605. * a recursive type definition needs to be interrupted to ensure type safety
  606. * and to avoid excessively deep recursion that could lead to performance issues.
  607. *
  608. * @internal
  609. */
  610. type InterruptRecursion = AnyNonNullishValue;
  611. /**
  612. * An if-else-like type that resolves depending on whether the given type is `never`.
  613. * This is mainly used to conditionally resolve the type of a `memoizeOptions` object based on whether `memoize` is provided or not.
  614. * @see {@link https://github.com/sindresorhus/type-fest/blob/main/source/if-never.d.ts Source}
  615. *
  616. * @internal
  617. */
  618. type IfNever<T, TypeIfNever, TypeIfNotNever> = [T] extends [never] ? TypeIfNever : TypeIfNotNever;
  619. /**
  620. * Omit any index signatures from the given object type, leaving only explicitly defined properties.
  621. * This is mainly used to remove explicit `any`s from the return type of some memoizers (e.g, `microMemoize`).
  622. *
  623. * __Disclaimer:__ When used on an intersection of a function and an object,
  624. * the function is erased.
  625. *
  626. * @see {@link https://github.com/sindresorhus/type-fest/blob/main/source/omit-index-signature.d.ts Source}
  627. *
  628. * @internal
  629. */
  630. type OmitIndexSignature<ObjectType> = {
  631. [KeyType in keyof ObjectType as {} extends Record<KeyType, unknown> ? never : KeyType]: ObjectType[KeyType];
  632. };
  633. /**
  634. * The infamous "convert a union type to an intersection type" hack
  635. * @see {@link https://github.com/sindresorhus/type-fest/blob/main/source/union-to-intersection.d.ts Source}
  636. * @see {@link https://github.com/microsoft/TypeScript/issues/29594 Reference}
  637. *
  638. * @internal
  639. */
  640. type UnionToIntersection<Union> = (Union extends unknown ? (distributedUnion: Union) => void : never) extends (mergedIntersection: infer Intersection) => void ? // The `& Union` is to allow indexing by the resulting type
  641. Intersection & Union : never;
  642. /**
  643. * Code to convert a union of values into a tuple.
  644. * @see {@link https://stackoverflow.com/a/55128956/62937 Source}
  645. *
  646. * @internal
  647. */
  648. type Push<T extends any[], V> = [...T, V];
  649. /**
  650. * @see {@link https://stackoverflow.com/a/55128956/62937 Source}
  651. *
  652. * @internal
  653. */
  654. type LastOf<T> = UnionToIntersection<T extends any ? () => T : never> extends () => infer R ? R : never;
  655. /**
  656. * TS4.1+
  657. * @see {@link https://stackoverflow.com/a/55128956/62937 Source}
  658. *
  659. * @internal
  660. */
  661. type TuplifyUnion<T, L = LastOf<T>, N = [T] extends [never] ? true : false> = true extends N ? [] : Push<TuplifyUnion<Exclude<T, L>>, L>;
  662. /**
  663. * Converts "the values of an object" into a tuple, like a type-level `Object.values()`
  664. * @see {@link https://stackoverflow.com/a/68695508/62937 Source}
  665. *
  666. * @internal
  667. */
  668. type ObjectValuesToTuple<T, KS extends any[] = TuplifyUnion<keyof T>, R extends any[] = []> = KS extends [infer K, ...infer KT] ? ObjectValuesToTuple<T, KT, [...R, T[K & keyof T]]> : R;
  669. /**
  670. * Create a type that makes the given keys required.
  671. * The remaining keys are kept as is.
  672. *
  673. * @see {@link https://github.com/sindresorhus/type-fest/blob/main/source/set-required.d.ts Source}
  674. *
  675. * @internal
  676. */
  677. type SetRequired<BaseType, Keys extends keyof BaseType> = Omit<BaseType, Keys> & Required<Pick<BaseType, Keys>>;
  678. /**
  679. * An if-else-like type that resolves depending on whether the given type is `unknown`.
  680. * @see {@link https://github.com/sindresorhus/type-fest/blob/main/source/if-unknown.d.ts Source}
  681. *
  682. * @internal
  683. */
  684. type IfUnknown<T, TypeIfUnknown, TypeIfNotUnknown> = unknown extends T ? [T] extends [null] ? TypeIfNotUnknown : TypeIfUnknown : TypeIfNotUnknown;
  685. /**
  686. * When a type is resolves to `unknown`, fallback to a different type.
  687. *
  688. * @template T - Type to be checked.
  689. * @template FallbackTo - Type to fallback to if `T` resolves to `unknown`.
  690. *
  691. * @internal
  692. */
  693. type FallbackIfUnknown<T, FallbackTo> = IfUnknown<T, FallbackTo, T>;
  694. /**
  695. * Useful to flatten the type output to improve type hints shown in editors.
  696. * And also to transform an interface into a type to aide with assignability.
  697. * @see {@link https://github.com/sindresorhus/type-fest/blob/main/source/simplify.d.ts Source}
  698. *
  699. * @internal
  700. */
  701. type Simplify<T> = T extends AnyFunction ? T : {
  702. [KeyType in keyof T]: T[KeyType];
  703. } & AnyNonNullishValue;
  704. /**
  705. * Uses an "auto-tracking" approach inspired by the work of the Ember Glimmer team.
  706. * It uses a Proxy to wrap arguments and track accesses to nested fields
  707. * in your selector on first read. Later, when the selector is called with
  708. * new arguments, it identifies which accessed fields have changed and
  709. * only recalculates the result if one or more of those accessed fields have changed.
  710. * This allows it to be more precise than the shallow equality checks in `lruMemoize`.
  711. *
  712. * __Design Tradeoffs for `autotrackMemoize`:__
  713. * - Pros:
  714. * - It is likely to avoid excess calculations and recalculate fewer times than `lruMemoize` will,
  715. * which may also result in fewer component re-renders.
  716. * - Cons:
  717. * - It only has a cache size of 1.
  718. * - It is slower than `lruMemoize`, because it has to do more work. (How much slower is dependent on the number of accessed fields in a selector, number of calls, frequency of input changes, etc)
  719. * - It can have some unexpected behavior. Because it tracks nested field accesses,
  720. * cases where you don't access a field will not recalculate properly.
  721. * For example, a badly-written selector like:
  722. * ```ts
  723. * createSelector([state => state.todos], todos => todos)
  724. * ```
  725. * that just immediately returns the extracted value will never update, because it doesn't see any field accesses to check.
  726. *
  727. * __Use Cases for `autotrackMemoize`:__
  728. * - It is likely best used for cases where you need to access specific nested fields
  729. * in data, and avoid recalculating if other fields in the same data objects are immutably updated.
  730. *
  731. * @param func - The function to be memoized.
  732. * @returns A memoized function with a `.clearCache()` method attached.
  733. *
  734. * @example
  735. * <caption>Using `createSelector`</caption>
  736. * ```ts
  737. * import { unstable_autotrackMemoize as autotrackMemoize, createSelector } from 'reselect'
  738. *
  739. * const selectTodoIds = createSelector(
  740. * [(state: RootState) => state.todos],
  741. * (todos) => todos.map(todo => todo.id),
  742. * { memoize: autotrackMemoize }
  743. * )
  744. * ```
  745. *
  746. * @example
  747. * <caption>Using `createSelectorCreator`</caption>
  748. * ```ts
  749. * import { unstable_autotrackMemoize as autotrackMemoize, createSelectorCreator } from 'reselect'
  750. *
  751. * const createSelectorAutotrack = createSelectorCreator({ memoize: autotrackMemoize })
  752. *
  753. * const selectTodoIds = createSelectorAutotrack(
  754. * [(state: RootState) => state.todos],
  755. * (todos) => todos.map(todo => todo.id)
  756. * )
  757. * ```
  758. *
  759. * @template Func - The type of the function that is memoized.
  760. *
  761. * @see {@link https://reselect.js.org/api/unstable_autotrackMemoize autotrackMemoize}
  762. *
  763. * @since 5.0.0
  764. * @public
  765. * @experimental
  766. */
  767. declare function autotrackMemoize<Func extends AnyFunction>(func: Func): Func & {
  768. clearCache: () => void;
  769. resultsCount: () => number;
  770. resetResultsCount: () => void;
  771. };
  772. /**
  773. * An instance of `createSelector`, customized with a given memoize implementation.
  774. *
  775. * @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`).
  776. * @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used.
  777. *
  778. * @public
  779. */
  780. interface CreateSelectorFunction<MemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize, ArgsMemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize> {
  781. /**
  782. * Creates a memoized selector function.
  783. *
  784. * @param createSelectorArgs - An arbitrary number of input selectors as separate inline arguments and a `combiner` function.
  785. * @returns A memoized output selector.
  786. *
  787. * @template InputSelectors - The type of the input selectors as an array.
  788. * @template Result - The return type of the `combiner` as well as the output selector.
  789. * @template OverrideMemoizeFunction - The type of the optional `memoize` function that could be passed into the options object to override the original `memoize` function that was initially passed into `createSelectorCreator`.
  790. * @template OverrideArgsMemoizeFunction - The type of the optional `argsMemoize` function that could be passed into the options object to override the original `argsMemoize` function that was initially passed into `createSelectorCreator`.
  791. *
  792. * @see {@link https://github.com/reduxjs/reselect#createselectorinputselectors--inputselectors-resultfunc-createselectoroptions createSelector}
  793. */
  794. <InputSelectors extends SelectorArray, Result>(...createSelectorArgs: [
  795. ...inputSelectors: InputSelectors,
  796. combiner: Combiner<InputSelectors, Result>
  797. ]): OutputSelector<InputSelectors, Result, MemoizeFunction, ArgsMemoizeFunction> & InterruptRecursion;
  798. /**
  799. * Creates a memoized selector function.
  800. *
  801. * @param createSelectorArgs - An arbitrary number of input selectors as separate inline arguments, a `combiner` function and an `options` object.
  802. * @returns A memoized output selector.
  803. *
  804. * @template InputSelectors - The type of the input selectors as an array.
  805. * @template Result - The return type of the `combiner` as well as the output selector.
  806. * @template OverrideMemoizeFunction - The type of the optional `memoize` function that could be passed into the options object to override the original `memoize` function that was initially passed into `createSelectorCreator`.
  807. * @template OverrideArgsMemoizeFunction - The type of the optional `argsMemoize` function that could be passed into the options object to override the original `argsMemoize` function that was initially passed into `createSelectorCreator`.
  808. *
  809. * @see {@link https://github.com/reduxjs/reselect#createselectorinputselectors--inputselectors-resultfunc-createselectoroptions createSelector}
  810. */
  811. <InputSelectors extends SelectorArray, Result, OverrideMemoizeFunction extends UnknownMemoizer = MemoizeFunction, OverrideArgsMemoizeFunction extends UnknownMemoizer = ArgsMemoizeFunction>(...createSelectorArgs: [
  812. ...inputSelectors: InputSelectors,
  813. combiner: Combiner<InputSelectors, Result>,
  814. createSelectorOptions: Simplify<CreateSelectorOptions<MemoizeFunction, ArgsMemoizeFunction, OverrideMemoizeFunction, OverrideArgsMemoizeFunction>>
  815. ]): OutputSelector<InputSelectors, Result, OverrideMemoizeFunction, OverrideArgsMemoizeFunction> & InterruptRecursion;
  816. /**
  817. * Creates a memoized selector function.
  818. *
  819. * @param inputSelectors - An array of input selectors.
  820. * @param combiner - A function that Combines the input selectors and returns an output selector. Otherwise known as the result function.
  821. * @param createSelectorOptions - An optional options object that allows for further customization per selector.
  822. * @returns A memoized output selector.
  823. *
  824. * @template InputSelectors - The type of the input selectors array.
  825. * @template Result - The return type of the `combiner` as well as the output selector.
  826. * @template OverrideMemoizeFunction - The type of the optional `memoize` function that could be passed into the options object to override the original `memoize` function that was initially passed into `createSelectorCreator`.
  827. * @template OverrideArgsMemoizeFunction - The type of the optional `argsMemoize` function that could be passed into the options object to override the original `argsMemoize` function that was initially passed into `createSelectorCreator`.
  828. *
  829. * @see {@link https://github.com/reduxjs/reselect#createselectorinputselectors--inputselectors-resultfunc-createselectoroptions createSelector}
  830. */
  831. <InputSelectors extends SelectorArray, Result, OverrideMemoizeFunction extends UnknownMemoizer = MemoizeFunction, OverrideArgsMemoizeFunction extends UnknownMemoizer = ArgsMemoizeFunction>(inputSelectors: [...InputSelectors], combiner: Combiner<InputSelectors, Result>, createSelectorOptions?: Simplify<CreateSelectorOptions<MemoizeFunction, ArgsMemoizeFunction, OverrideMemoizeFunction, OverrideArgsMemoizeFunction>>): OutputSelector<InputSelectors, Result, OverrideMemoizeFunction, OverrideArgsMemoizeFunction> & InterruptRecursion;
  832. }
  833. /**
  834. * Creates a selector creator function with the specified memoization function
  835. * and options for customizing memoization behavior.
  836. *
  837. * @param options - An options object containing the `memoize` function responsible for memoizing the `resultFunc` inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`). It also provides additional options for customizing memoization. While the `memoize` property is mandatory, the rest are optional.
  838. * @returns A customized `createSelector` function.
  839. *
  840. * @example
  841. * ```ts
  842. * const customCreateSelector = createSelectorCreator({
  843. * memoize: customMemoize, // Function to be used to memoize `resultFunc`
  844. * memoizeOptions: [memoizeOption1, memoizeOption2], // Options passed to `customMemoize` as the second argument onwards
  845. * argsMemoize: customArgsMemoize, // Function to be used to memoize the selector's arguments
  846. * argsMemoizeOptions: [argsMemoizeOption1, argsMemoizeOption2] // Options passed to `customArgsMemoize` as the second argument onwards
  847. * })
  848. *
  849. * const customSelector = customCreateSelector(
  850. * [inputSelector1, inputSelector2],
  851. * resultFunc // `resultFunc` will be passed as the first argument to `customMemoize`
  852. * )
  853. *
  854. * customSelector(
  855. * ...selectorArgs // Will be memoized by `customArgsMemoize`
  856. * )
  857. * ```
  858. *
  859. * @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`).
  860. * @template ArgsMemoizeFunction - The type of the optional memoize function that is used to memoize the arguments passed into the output selector generated by `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`). If none is explicitly provided, `weakMapMemoize` will be used.
  861. *
  862. * @see {@link https://reselect.js.org/api/createSelectorCreator#using-options-since-500 `createSelectorCreator`}
  863. *
  864. * @since 5.0.0
  865. * @public
  866. */
  867. declare function createSelectorCreator<MemoizeFunction extends UnknownMemoizer, ArgsMemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize>(options: Simplify<SetRequired<CreateSelectorOptions<typeof weakMapMemoize, typeof weakMapMemoize, MemoizeFunction, ArgsMemoizeFunction>, 'memoize'>>): CreateSelectorFunction<MemoizeFunction, ArgsMemoizeFunction>;
  868. /**
  869. * Creates a selector creator function with the specified memoization function
  870. * and options for customizing memoization behavior.
  871. *
  872. * @param memoize - The `memoize` function responsible for memoizing the `resultFunc` inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`).
  873. * @param memoizeOptionsFromArgs - Optional configuration options for the memoization function. These options are then passed to the memoize function as the second argument onwards.
  874. * @returns A customized `createSelector` function.
  875. *
  876. * @example
  877. * ```ts
  878. * const customCreateSelector = createSelectorCreator(customMemoize, // Function to be used to memoize `resultFunc`
  879. * option1, // Will be passed as second argument to `customMemoize`
  880. * option2, // Will be passed as third argument to `customMemoize`
  881. * option3 // Will be passed as fourth argument to `customMemoize`
  882. * )
  883. *
  884. * const customSelector = customCreateSelector(
  885. * [inputSelector1, inputSelector2],
  886. * resultFunc // `resultFunc` will be passed as the first argument to `customMemoize`
  887. * )
  888. * ```
  889. *
  890. * @template MemoizeFunction - The type of the memoize function that is used to memoize the `resultFunc` inside `createSelector` (e.g., `lruMemoize` or `weakMapMemoize`).
  891. *
  892. * @see {@link https://reselect.js.org/api/createSelectorCreator#using-memoize-and-memoizeoptions `createSelectorCreator`}
  893. *
  894. * @public
  895. */
  896. declare function createSelectorCreator<MemoizeFunction extends UnknownMemoizer>(memoize: MemoizeFunction, ...memoizeOptionsFromArgs: DropFirstParameter<MemoizeFunction>): CreateSelectorFunction<MemoizeFunction>;
  897. /**
  898. * Accepts one or more "input selectors" (either as separate arguments or a single array),
  899. * a single "result function" / "combiner", and an optional options object, and
  900. * generates a memoized selector function.
  901. *
  902. * @see {@link https://reselect.js.org/api/createSelector `createSelector`}
  903. *
  904. * @public
  905. */
  906. declare const createSelector: CreateSelectorFunction<typeof weakMapMemoize, typeof weakMapMemoize>;
  907. /**
  908. *
  909. * @WIP
  910. */
  911. type SelectorsMap<T extends SelectorsObject> = {
  912. [Key in keyof T]: ReturnType<T[Key]>;
  913. };
  914. /**
  915. * Allows you to create a pre-typed version of {@linkcode createStructuredSelector createStructuredSelector}
  916. * For your root state.
  917. *
  918. * @since 5.0.0
  919. * @public
  920. * @WIP
  921. */
  922. interface TypedStructuredSelectorCreator<RootState = any> {
  923. <InputSelectorsObject extends {
  924. [Key in keyof RootState]: Selector<RootState, RootState[Key], []>;
  925. } = {
  926. [Key in keyof RootState]: Selector<RootState, RootState[Key], []>;
  927. }, MemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize, ArgsMemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize>(selectors: InputSelectorsObject, selectorCreator?: CreateSelectorFunction<MemoizeFunction, ArgsMemoizeFunction>): OutputSelector<ObjectValuesToTuple<InputSelectorsObject>, SelectorsMap<InputSelectorsObject>, MemoizeFunction, ArgsMemoizeFunction>;
  928. }
  929. interface SelectorsObject {
  930. [key: string]: Selector;
  931. }
  932. /**
  933. * It provides a way to create structured selectors.
  934. * The structured selector can take multiple input selectors
  935. * and map their output to an object with specific keys.
  936. *
  937. * @see {@link https://reselect.js.org/api/createStructuredSelector `createStructuredSelector`}
  938. *
  939. * @public
  940. */
  941. interface StructuredSelectorCreator {
  942. /**
  943. * A convenience function that simplifies returning an object
  944. * made up of selector results.
  945. *
  946. * @param inputSelectorsObject - A key value pair consisting of input selectors.
  947. * @param selectorCreator - A custom selector creator function. It defaults to `createSelector`.
  948. * @returns A memoized structured selector.
  949. *
  950. * @example
  951. * <caption>Modern Use Case</caption>
  952. * ```ts
  953. * import { createSelector, createStructuredSelector } from 'reselect'
  954. *
  955. * interface RootState {
  956. * todos: {
  957. * id: number
  958. * completed: boolean
  959. * title: string
  960. * description: string
  961. * }[]
  962. * alerts: { id: number; read: boolean }[]
  963. * }
  964. *
  965. * // This:
  966. * const structuredSelector = createStructuredSelector(
  967. * {
  968. * todos: (state: RootState) => state.todos,
  969. * alerts: (state: RootState) => state.alerts,
  970. * todoById: (state: RootState, id: number) => state.todos[id]
  971. * },
  972. * createSelector
  973. * )
  974. *
  975. * // Is essentially the same as this:
  976. * const selector = createSelector(
  977. * [
  978. * (state: RootState) => state.todos,
  979. * (state: RootState) => state.alerts,
  980. * (state: RootState, id: number) => state.todos[id]
  981. * ],
  982. * (todos, alerts, todoById) => {
  983. * return {
  984. * todos,
  985. * alerts,
  986. * todoById
  987. * }
  988. * }
  989. * )
  990. * ```
  991. *
  992. * @example
  993. * <caption>Simple Use Case</caption>
  994. * ```ts
  995. * const selectA = state => state.a
  996. * const selectB = state => state.b
  997. *
  998. * // The result function in the following selector
  999. * // is simply building an object from the input selectors
  1000. * const structuredSelector = createSelector(selectA, selectB, (a, b) => ({
  1001. * a,
  1002. * b
  1003. * }))
  1004. *
  1005. * const result = structuredSelector({ a: 1, b: 2 }) // will produce { x: 1, y: 2 }
  1006. * ```
  1007. *
  1008. * @template InputSelectorsObject - The shape of the input selectors object.
  1009. * @template MemoizeFunction - The type of the memoize function that is used to create the structured selector. It defaults to `weakMapMemoize`.
  1010. * @template ArgsMemoizeFunction - The type of the of the memoize function that is used to memoize the arguments passed into the generated structured selector. It defaults to `weakMapMemoize`.
  1011. *
  1012. * @see {@link https://reselect.js.org/api/createStructuredSelector `createStructuredSelector`}
  1013. */
  1014. <InputSelectorsObject extends SelectorsObject, MemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize, ArgsMemoizeFunction extends UnknownMemoizer = typeof weakMapMemoize>(inputSelectorsObject: InputSelectorsObject, selectorCreator?: CreateSelectorFunction<MemoizeFunction, ArgsMemoizeFunction>): OutputSelector<ObjectValuesToTuple<InputSelectorsObject>, Simplify<SelectorsMap<InputSelectorsObject>>, MemoizeFunction, ArgsMemoizeFunction> & InterruptRecursion;
  1015. }
  1016. /**
  1017. * A convenience function that simplifies returning an object
  1018. * made up of selector results.
  1019. *
  1020. * @example
  1021. * <caption>Modern Use Case</caption>
  1022. * ```ts
  1023. * import { createSelector, createStructuredSelector } from 'reselect'
  1024. *
  1025. * interface RootState {
  1026. * todos: {
  1027. * id: number
  1028. * completed: boolean
  1029. * title: string
  1030. * description: string
  1031. * }[]
  1032. * alerts: { id: number; read: boolean }[]
  1033. * }
  1034. *
  1035. * // This:
  1036. * const structuredSelector = createStructuredSelector(
  1037. * {
  1038. * todos: (state: RootState) => state.todos,
  1039. * alerts: (state: RootState) => state.alerts,
  1040. * todoById: (state: RootState, id: number) => state.todos[id]
  1041. * },
  1042. * createSelector
  1043. * )
  1044. *
  1045. * // Is essentially the same as this:
  1046. * const selector = createSelector(
  1047. * [
  1048. * (state: RootState) => state.todos,
  1049. * (state: RootState) => state.alerts,
  1050. * (state: RootState, id: number) => state.todos[id]
  1051. * ],
  1052. * (todos, alerts, todoById) => {
  1053. * return {
  1054. * todos,
  1055. * alerts,
  1056. * todoById
  1057. * }
  1058. * }
  1059. * )
  1060. * ```
  1061. *
  1062. * @see {@link https://reselect.js.org/api/createStructuredSelector `createStructuredSelector`}
  1063. *
  1064. * @public
  1065. */
  1066. declare const createStructuredSelector: StructuredSelectorCreator;
  1067. /**
  1068. * Overrides the development mode checks settings for all selectors.
  1069. *
  1070. * Reselect performs additional checks in development mode to help identify and
  1071. * warn about potential issues in selector behavior. This function allows you to
  1072. * customize the behavior of these checks across all selectors in your application.
  1073. *
  1074. * **Note**: This setting can still be overridden per selector inside `createSelector`'s `options` object.
  1075. * See {@link https://github.com/reduxjs/reselect#2-per-selector-by-passing-an-identityfunctioncheck-option-directly-to-createselector per-selector-configuration}
  1076. * and {@linkcode CreateSelectorOptions.identityFunctionCheck identityFunctionCheck} for more details.
  1077. *
  1078. * _The development mode checks do not run in production builds._
  1079. *
  1080. * @param devModeChecks - An object specifying the desired settings for development mode checks. You can provide partial overrides. Unspecified settings will retain their current values.
  1081. *
  1082. * @example
  1083. * ```ts
  1084. * import { setGlobalDevModeChecks } from 'reselect'
  1085. * import { DevModeChecks } from '../types'
  1086. *
  1087. * // Run only the first time the selector is called. (default)
  1088. * setGlobalDevModeChecks({ inputStabilityCheck: 'once' })
  1089. *
  1090. * // Run every time the selector is called.
  1091. * setGlobalDevModeChecks({ inputStabilityCheck: 'always' })
  1092. *
  1093. * // Never run the input stability check.
  1094. * setGlobalDevModeChecks({ inputStabilityCheck: 'never' })
  1095. *
  1096. * // Run only the first time the selector is called. (default)
  1097. * setGlobalDevModeChecks({ identityFunctionCheck: 'once' })
  1098. *
  1099. * // Run every time the selector is called.
  1100. * setGlobalDevModeChecks({ identityFunctionCheck: 'always' })
  1101. *
  1102. * // Never run the identity function check.
  1103. * setGlobalDevModeChecks({ identityFunctionCheck: 'never' })
  1104. * ```
  1105. * @see {@link https://reselect.js.org/api/development-only-stability-checks Development-Only Stability Checks}
  1106. * @see {@link https://reselect.js.org/api/development-only-stability-checks#1-globally-through-setglobaldevmodechecks global-configuration}
  1107. *
  1108. * @since 5.0.0
  1109. * @public
  1110. */
  1111. declare const setGlobalDevModeChecks: (devModeChecks: Partial<DevModeChecks>) => void;
  1112. /**
  1113. * Runs a simple reference equality check.
  1114. * What {@linkcode lruMemoize lruMemoize} uses by default.
  1115. *
  1116. * **Note**: This function was previously known as `defaultEqualityCheck`.
  1117. *
  1118. * @public
  1119. */
  1120. declare const referenceEqualityCheck: EqualityFn;
  1121. /**
  1122. * Options for configuring the behavior of a function memoized with
  1123. * LRU (Least Recently Used) caching.
  1124. *
  1125. * @template Result - The type of the return value of the memoized function.
  1126. *
  1127. * @public
  1128. */
  1129. interface LruMemoizeOptions<Result = any> {
  1130. /**
  1131. * Function used to compare the individual arguments of the
  1132. * provided calculation function.
  1133. *
  1134. * @default referenceEqualityCheck
  1135. */
  1136. equalityCheck?: EqualityFn;
  1137. /**
  1138. * If provided, used to compare a newly generated output value against
  1139. * previous values in the cache. If a match is found,
  1140. * the old value is returned. This addresses the common
  1141. * ```ts
  1142. * todos.map(todo => todo.id)
  1143. * ```
  1144. * use case, where an update to another field in the original data causes
  1145. * a recalculation due to changed references, but the output is still
  1146. * effectively the same.
  1147. *
  1148. * @since 4.1.0
  1149. */
  1150. resultEqualityCheck?: EqualityFn<Result>;
  1151. /**
  1152. * The maximum size of the cache used by the selector.
  1153. * A size greater than 1 means the selector will use an
  1154. * LRU (Least Recently Used) cache, allowing for the caching of multiple
  1155. * results based on different sets of arguments.
  1156. *
  1157. * @default 1
  1158. */
  1159. maxSize?: number;
  1160. }
  1161. /**
  1162. * Creates a memoized version of a function with an optional
  1163. * LRU (Least Recently Used) cache. The memoized function uses a cache to
  1164. * store computed values. Depending on the `maxSize` option, it will use
  1165. * either a singleton cache (for a single entry) or an
  1166. * LRU cache (for multiple entries).
  1167. *
  1168. * **Note**: This function was previously known as `defaultMemoize`.
  1169. *
  1170. * @param func - The function to be memoized.
  1171. * @param equalityCheckOrOptions - Either an equality check function or an options object.
  1172. * @returns A memoized function with a `.clearCache()` method attached.
  1173. *
  1174. * @template Func - The type of the function that is memoized.
  1175. *
  1176. * @see {@link https://reselect.js.org/api/lruMemoize `lruMemoize`}
  1177. *
  1178. * @public
  1179. */
  1180. declare function lruMemoize<Func extends AnyFunction>(func: Func, equalityCheckOrOptions?: EqualityFn | LruMemoizeOptions<ReturnType<Func>>): Func & {
  1181. clearCache: () => void;
  1182. resultsCount: () => number;
  1183. resetResultsCount: () => void;
  1184. };
  1185. export { Combiner, CreateSelectorFunction, CreateSelectorOptions, DefaultMemoizeFields, DevModeCheckFrequency, DevModeChecks, DevModeChecksExecutionInfo, EqualityFn, ExtractMemoizerFields, GetParamsFromSelectors, GetStateFromSelectors, LruMemoizeOptions, MemoizeOptionsFromParameters, OutputSelector, OutputSelectorFields, OverrideMemoizeOptions, Selector, SelectorArray, SelectorResultArray, StructuredSelectorCreator, TypedStructuredSelectorCreator, UnknownMemoizer, WeakMapMemoizeOptions, createSelector, createSelectorCreator, createStructuredSelector, lruMemoize, referenceEqualityCheck, setGlobalDevModeChecks, autotrackMemoize as unstable_autotrackMemoize, weakMapMemoize };