3

I am trying out PanResponder, and started off of the example in the React Native documentation. I'll copy part of the example code here:

const Example = () => {
  const pan = useRef(new Animated.ValueXY()).current;

  const panResponder = useRef(
    PanResponder.create({
      onMoveShouldSetPanResponder: () => true,
      onPanResponderGrant: () => {
        pan.setOffset({
          x: pan.x._value,
                   ^^^^^^
          y: pan.y._value,
                   ^^^^^^
        });
      },
      onPanResponderMove: Animated.event([null, { dx: pan.x, dy: pan.y }]),
      onPanResponderRelease: () => {
        pan.flattenOffset();
      },
    })
  ).current;

Problem is, I get an TS error in WebStorm I wasn't expecting (this being the official example code). Here's the exact error:

TS error

How do I get rid of this?

Thanks.

raarts
  • 2,711
  • 4
  • 25
  • 45

1 Answers1

3

when you are initialising the useRef hook use type any. this will remove the error. i think react native doc is not updated to show examples with typescript.

  const pan = useRef<any>(new Animated.ValueXY()).current;
Ushan Fernando
  • 622
  • 4
  • 19
  • 3
    any is an anti-pattern. You can 'solve' a lot with it, but it's never the real solution. – raarts Jul 23 '22 at 21:34
  • 2
    yeah using any is not a good practice but in this case there is no any other way to get rid of this error. we cant access a private variable with ts like explained in the code. react native dev team should update the PanResponder class to compatible with ts. – Ushan Fernando Jul 25 '22 at 06:27