From 99a7c9efcb2a7dc670d36324a75b2fdfb85fbe0d Mon Sep 17 00:00:00 2001 From: Arttu Olli Date: Sat, 22 Oct 2022 22:43:14 +0300 Subject: [PATCH] fix(useCustomCompareMemo): Correctly infer the type of the value returned by the factory function Change the return type to the type returned by the factory function instead of unknown. This makes the hook work the same way as useMemo. fix #975 --- src/useCustomCompareMemo/__docs__/story.mdx | 6 +++--- src/useCustomCompareMemo/useCustomCompareMemo.ts | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/useCustomCompareMemo/__docs__/story.mdx b/src/useCustomCompareMemo/__docs__/story.mdx index e9e4bd1d..fdcf3a8b 100644 --- a/src/useCustomCompareMemo/__docs__/story.mdx +++ b/src/useCustomCompareMemo/__docs__/story.mdx @@ -22,11 +22,11 @@ export type DependenciesComparator b: Deps ) => boolean; -function useCustomCompareMemo unknown, Deps extends DependencyList>( - factory: Factory, +function useCustomCompareMemo( + factory: () => T, deps: Deps, comparator: DependenciesComparator -): ReturnType; +): T ``` #### Importing diff --git a/src/useCustomCompareMemo/useCustomCompareMemo.ts b/src/useCustomCompareMemo/useCustomCompareMemo.ts index 6facbf39..63be76ae 100644 --- a/src/useCustomCompareMemo/useCustomCompareMemo.ts +++ b/src/useCustomCompareMemo/useCustomCompareMemo.ts @@ -10,11 +10,11 @@ import type { DependenciesComparator } from '../types'; * @param comparator function to validate dependency changes * @returns useMemo result */ -export const useCustomCompareMemo = unknown, Deps extends DependencyList>( - factory: Factory, +export const useCustomCompareMemo = ( + factory: () => T, deps: Deps, comparator: DependenciesComparator -) => { +): T => { const dependencies = useRef(); if (dependencies.current === undefined || !comparator(dependencies.current, deps)) { @@ -22,5 +22,5 @@ export const useCustomCompareMemo = unknown, Deps extends } // eslint-disable-next-line react-hooks/exhaustive-deps -- missing factory function - return useMemo(factory, dependencies.current); + return useMemo(factory, dependencies.current); };