Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(useCustomCompareMemo): Correctly infer factory function return type #976

Merged
merged 1 commit into from
Oct 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/useCustomCompareMemo/__docs__/story.mdx
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
};