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

Simplify extended useDebouncedCallback function types #140

Merged
merged 1 commit into from
Aug 27, 2022

Conversation

sarunast
Copy link
Contributor

@sarunast sarunast commented Aug 11, 2022

The types are failing if the function is used with other generic functions.
For example type <Args extends Array<unknown> fails this check.

A minimal example of the failing types that this change would fix:

import type { ActionCreatorWithPreparedPayload } from "@reduxjs/toolkit";
import { useDispatch } from "react-redux";
import { useDebouncedCallback } from "use-debounce";

interface UseFilterUpdaterParams<Args extends Array<unknown>, Payload> {
  updateFunction: ActionCreatorWithPreparedPayload<Args, Payload>;
  DEBOUNCED_HANDLER_MS?: number;
}

const useUpdater = <Args extends Array<unknown>, Payload>({
  updateFunction,
  DEBOUNCED_HANDLER_MS = 0,
}: UseFilterUpdaterParams<Args, Payload>) => {
  const dispatch = useDispatch();

  /**
   * Argument of type '(...props: Args) => void' is not assignable to parameter of type '(...args: any[]) => void'.
   *   Types of parameters 'props' and 'args' are incompatible.
   *     Type 'any[]' is not assignable to type 'Args'.
   *       'any[]' is assignable to the constraint of type 'Args', but 'Args' could be instantiated with a different subtype of constraint 'unknown[]'.(2345)
   */
  const debouncedCallback = useDebouncedCallback((...props: Args) => {
    dispatch(updateFunction(...props));
  }, DEBOUNCED_HANDLER_MS);

  return debouncedCallback;
};

export default useUpdater;

Reproducible example: Typescript playground

The types are failing if the function is used with other generic functions.
For example type `<Args extends Array<unknown>` fails this check.
@sarunast sarunast changed the title Simplify extended useCallback function types Simplify extended useDebouncedCallback function types Aug 11, 2022
@xnimorz
Copy link
Owner

xnimorz commented Aug 27, 2022

Hey @sarunast
Sorry for the log response.

Thank you for the PR.
I'll cut a new release shortly

@xnimorz xnimorz merged commit 3877446 into xnimorz:master Aug 27, 2022
@xnimorz
Copy link
Owner

xnimorz commented Aug 28, 2022

Published in [email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants