diff --git a/src/index.ts b/src/index.ts index fd90aeab..58323c42 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,6 +17,7 @@ import { TapCallback, UP, Vector2, + OnSwipedDirs, } from "./types"; export { @@ -183,10 +184,8 @@ function getHandlers( eventData = { ...state.eventData, event }; props.onSwiped && props.onSwiped(eventData); - const onSwipedDir = `onSwiped${eventData.dir}`; - if (onSwipedDir in props) { - ((props as any)[onSwipedDir] as SwipeCallback)(eventData); - } + const onSwipedDir = `onSwiped${eventData.dir}` as OnSwipedDirs; + props[onSwipedDir]?.(eventData); } else { props.onTap && props.onTap({ event }); } diff --git a/src/types.ts b/src/types.ts index 77785d2b..ed5c232d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -11,6 +11,7 @@ export type SwipeDirections = | typeof RIGHT | typeof UP | typeof DOWN; +export type OnSwipedDirs = `onSwiped${SwipeDirections}`; export interface SwipeEventData { absX: number; absY: number; @@ -27,13 +28,13 @@ export interface SwipeEventData { export type SwipeCallback = (eventData: SwipeEventData) => void; export type TapCallback = ({ event }: { event: HandledEvents }) => void; -export type SwipeableCallbacks = { +type SwipedCallbacks = { + [K in OnSwipedDirs]?: SwipeCallback; +} + +export type SwipeableCallbacks = SwipedCallbacks & { // Event handler/callbacks onSwiped: SwipeCallback; - onSwipedDown: SwipeCallback; - onSwipedLeft: SwipeCallback; - onSwipedRight: SwipeCallback; - onSwipedUp: SwipeCallback; onSwiping: SwipeCallback; onTap: TapCallback; };