skip to Main Content

I want fix all errors
I have this error 1
error

And this error 2
error 2

My code

function onMouseTouchDown(e: React.SyntheticEvent<HTMLDivElement>) {
    e.preventDefault()
    setDown(true)

    if (e.type === "touchmove" && e.changedTouches) {
      setX(e.changedTouches[0].pageX - e.currentTarget.offsetLeft)
    } else {
      setX(e.pageX - e.currentTarget.offsetLeft)
    }

    setScrollLeft(e.currentTarget.scrollLeft)
  }

codesandbox – https://codesandbox.io/p/sandbox/slider-jc-consoleerror-type-error-tktvxn?file=%2Fsrc%2Fhooks%2FuseSlider.ts%3A49%2C6

Help me please fix this error

2

Answers


  1. Chosen as BEST ANSWER

    To search in which property exist some type go to developer.mozilla.org and search enter image description here

    enter image description here And include it to your interface
    enter image description here

    If you need you can extend some type to your interface e.g TouchEvent


  2. You can modify your code like this:

    function onMouseTouchDown(e: React.SyntheticEvent<HTMLDivElement>) {
      /* ... */
      if (e.type === "touchmove" && (e as any).changedTouches) {
        setX((e as any).changedTouches[0].pageX - e.currentTarget.offsetLeft);
      } else {
        setX((e as any).pageX - e.currentTarget.offsetLeft);
      }
        /* ... */
    }
    
    
    function move(
      e: React.MouseEvent<HTMLDivElement> | React.TouchEvent<HTMLDivElement>
    ) {
      /* ... */
      if (e.type === "touchmove" && (e as any).changedTouches) {
        x = (e as any).changedTouches[0].pageX - e.currentTarget.offsetLeft;
      } else {
        x = (e as any).pageX - e.currentTarget.offsetLeft;
      }
      /* ... */
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search