Skip to content

Commit

Permalink
fix(useCustomCompareMemo): Correctly infer the type of the value retu…
Browse files Browse the repository at this point in the history
…rned by the factory function (#976)

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
  • Loading branch information
ArttuOll committed Oct 22, 2022
1 parent 02745ff commit a625c55
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/useCustomCompareMemo/__docs__/story.mdx
Expand Up @@ -22,11 +22,11 @@ export type DependenciesComparator<Deps extends DependencyList = DependencyList>
b: Deps
) => boolean;

function useCustomCompareMemo<Factory extends () => unknown, Deps extends DependencyList>(
factory: Factory,
function useCustomCompareMemo<T, Deps extends DependencyList>(
factory: () => T,
deps: Deps,
comparator: DependenciesComparator<Deps>
): ReturnType<Factory>;
): T
```

#### Importing
Expand Down
8 changes: 4 additions & 4 deletions src/useCustomCompareMemo/useCustomCompareMemo.ts
Expand Up @@ -10,17 +10,17 @@ import type { DependenciesComparator } from '../types';
* @param comparator function to validate dependency changes
* @returns useMemo result
*/
export const useCustomCompareMemo = <Factory extends () => unknown, Deps extends DependencyList>(
factory: Factory,
export const useCustomCompareMemo = <T, Deps extends DependencyList>(
factory: () => T,
deps: Deps,
comparator: DependenciesComparator<Deps>
) => {
): T => {
const dependencies = useRef<Deps>();

if (dependencies.current === undefined || !comparator(dependencies.current, deps)) {
dependencies.current = deps;
}

// eslint-disable-next-line react-hooks/exhaustive-deps -- missing factory function
return useMemo(factory, dependencies.current);
return useMemo<T>(factory, dependencies.current);
};

0 comments on commit a625c55

Please sign in to comment.