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

Typescript error when using useSwipable hook and applying handlers. #140

Closed
domhede opened this issue May 13, 2019 · 4 comments · Fixed by #142
Closed

Typescript error when using useSwipable hook and applying handlers. #140

domhede opened this issue May 13, 2019 · 4 comments · Fixed by #142
Labels

Comments

@domhede
Copy link

domhede commented May 13, 2019

Using the hook, I have typed the handlers as SwipeableHandlers and spread them on to the swipeable div. However this results in a Typescript error. Any help would be most appreciated. Thanks

Error

Type '{ children: Element[]; ref: ({}: HTMLElement) => void; onMouseDown?: ((event: MouseEvent<Element, MouseEvent>) => void) | undefined; className: string; }' is not assignable to type 'DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>'.
  Type '{ children: Element[]; ref: ({}: HTMLElement) => void; onMouseDown?: ((event: MouseEvent<Element, MouseEvent>) => void) | undefined; className: string; }' is not assignable to type 'ClassAttributes<HTMLDivElement>'.
    Types of property 'ref' are incompatible.
      Type '({}: HTMLElement) => void' is not assignable to type 'string | ((instance: HTMLDivElement | null) => void) | RefObject<HTMLDivElement> | null | undefined'.
        Type '({}: HTMLElement) => void' is not assignable to type '(instance: HTMLDivElement | null) => void'.
          Types of parameters '__0' and 'instance' are incompatible.
            Type 'HTMLDivElement | null' is not assignable to type 'HTMLElement'.
              Type 'null' is not assignable to type 'HTMLElement'.ts(2322)

Code

    const handlers: SwipeableHandlers = useSwipeable({
        onSwipedLeft: handleOnSwipedLeft,
        onSwipedRight: handleOnSwipedRight,
        onSwiped: handleOnSwiped,
        onSwiping: handleSwiping,
        delta: horizontalSwipeThreshold,
        preventDefaultTouchmoveEvent: true,
        trackTouch: true,
    });

    return (
        <div {...handlers}>
           ...
        </div>
    );
@domhede
Copy link
Author

domhede commented May 13, 2019

I suspect the type definition for SwipeHandlers may not be correct. Allowing for null fixes the error.

ie.

export interface SwipeableHandlers {
    ref: ({  }: HTMLElement | null) => void;
    onMouseDown?: React.MouseEventHandler;
}

@hartzis
Copy link
Collaborator

hartzis commented May 13, 2019

@domhede Thanks for the issue and finding a possible solution. Able to make a PR?

Do you have any insight as to why our test passes? What version of typescript are you using?

Test: https://github.com/dogfessional/react-swipeable/blob/master/types/test.tsx#L54-L58

@hartzis
Copy link
Collaborator

hartzis commented May 14, 2019

@domhede Do you know if this is the root issue, from tslint, palantir/tslint#4589?

@domhede
Copy link
Author

domhede commented May 14, 2019

@hartzis I don't think so, the error message seems to point to the types of ref not matching. As to why the test still pass, I don't think it's to do with the TS version but rather the linting rules/config.

mastermatt added a commit to mastermatt/react-swipeable that referenced this issue May 14, 2019
Fixes FormidableLabs#140.

Adds test to explicitly cover this case because it seems `dtslint`
doesn't catch the issue when a handlers instance is spread during an
`Element` instantiation. The React types do [declare the arg correctly](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/index.d.ts#L85).
mastermatt added a commit to mastermatt/react-swipeable that referenced this issue May 15, 2019
Fixes FormidableLabs#140.

Adds test to explicitly cover this case because it seems `dtslint`
doesn't catch the issue when a handlers instance is spread during an
`Element` instantiation. The React types do [declare the arg correctly](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/index.d.ts#L85).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants