memoize-one
Version:
A memoization library which only remembers the latest invocation
45 lines • 2.26 kB
TypeScript
/**
* A function that decides if the new arguments are equal to the last arguments.
* Return `true` to use the cached result, or `false` to call the result function again.
*
* `TFunc` is the function being memoized. Without it, `EqualityFn` accepts the
* arguments of any function, so you can write one equality function and reuse it.
*/
export type EqualityFn<TFunc extends (...args: any[]) => any = (...args: any[]) => any> = (newArgs: Parameters<TFunc>, lastArgs: Parameters<TFunc>) => boolean;
/**
* A memoized version of `TFunc`. It has the same `this` type, parameters and
* return type as `TFunc`, plus a `.clear()` function to clear the cache.
*
* Other properties of `TFunc` are not copied. Because the call signature is
* rebuilt from `Parameters` and `ReturnType`, only the last overload of `TFunc`
* is kept, generic type parameters become `unknown`, and type predicates and
* assertion signatures no longer narrow.
*/
export type MemoizedFn<TFunc extends (this: any, ...args: any[]) => any> = {
/** Clear the cache, so that the next call will call the result function again */
clear: () => void;
(this: ThisParameterType<TFunc>, ...args: Parameters<TFunc>): ReturnType<TFunc>;
};
/**
* The default equality check used by `memoizeOne`.
*
* Returns `true` when both lists have the same length,
* and each item is the same value according to `Object.is`.
*/
export declare function areInputsEqual(newInputs: readonly unknown[], lastInputs: readonly unknown[]): boolean;
/**
* Memoize a function so that it only remembers the latest arguments and result.
*
* The result function is called again when `this` or the arguments change.
* By default, arguments are compared with `areInputsEqual` (`Object.is` on each argument).
*
* @param resultFn The function to memoize
* @param isEqual An optional custom equality function for the arguments
*/
export declare function memoizeOne<TFunc extends (this: any, ...newArgs: any[]) => any>(resultFn: TFunc, isEqual?: EqualityFn<TFunc & {}>): MemoizedFn<TFunc>;
/**
* The same function as the named `memoizeOne` export.
* Kept so that `import memoizeOne from 'memoize-one'` from v6 still works.
*/
export default memoizeOne;
//# sourceMappingURL=memoize-one.d.ts.map