From 45e9ae1815969f4fb01deeae11b73666a62f76b2 Mon Sep 17 00:00:00 2001 From: Emil Hartz Date: Mon, 1 Mar 2021 16:49:19 -0700 Subject: [PATCH] attempt to use Template Literal Types --- src/index.ts | 7 +++---- src/types.ts | 11 ++++++----- 2 files changed, 9 insertions(+), 9 deletions(-) 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; };