M
M
maestro072021-07-22 15:04:53
typescript
maestro07, 2021-07-22 15:04:53

How to find out the type of variables?

Rewriting (range) component from JS to TS.
As I understand, there are two cases, for the desktop and for the mobile version of the

difficulty in determining the type of the parameters of the method.

const handleThumbEnd = (eventData) => thumbHandler(eventData.event, true);
  const handleThumbMove = useThrottledFn(
    (eventData) => thumbHandler(eventData.event),
    250,
  );

  const handlers = useSwipeable({
    onSwiping: handleThumbMove,
    onSwiped: handleThumbEnd,
    preventDefaultTouchmoveEvent: true,
    trackMouse: true,
    trackTouch: true,
  });


eventData specified both MouseEvent and TouchEvent. but I still get an error that eventData.event does not have such a property

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry X, 2021-07-22
@1MK-Ultra

something like this:
if (myvar==true ) { }
if (myvar>1) { }
etc.
where it will not give an error, then this type.

L
Leonid Yakovlev, 2021-07-22
@deleo547

Using Beautiful React Hooks and React Swipeable?

D
Dmitry Belyaev, 2021-07-23
@bingo347

If either does not export the types it needs, then I would try this:

type EventData = {
    event: Parameters<typeof thumbHandler>[0];
};

const handleThumbEnd = (eventData: EventData) => thumbHandler(eventData.event, true);
const handleThumbMove = useThrottledFn(
    (eventData: EventData) => thumbHandler(eventData.event),
    250,
);

const handlers = useSwipeable({
    onSwiping: handleThumbMove,
    onSwiped: handleThumbEnd,
    preventDefaultTouchmoveEvent: true,
    trackMouse: true,
    trackTouch: true,
});
or like this:
type HandlerMove = Parameters<typeof useSwipeable>[0]['onSwiping'];
type HandlerEnd = Parameters<typeof useSwipeable>[0]['onSwiped'];

const handleThumbEnd: HandlerMove = (eventData) => thumbHandler(eventData.event, true);
const handleThumbMove: HandlerEnd = useThrottledFn(
    (eventData) => thumbHandler(eventData.event),
    250,
);

const handlers = useSwipeable({
    onSwiping: handleThumbMove,
    onSwiped: handleThumbEnd,
    preventDefaultTouchmoveEvent: true,
    trackMouse: true,
    trackTouch: true,
});
or so

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question