Skip to content

Commit 9c7c911

Browse files
yfunkJakub Gonet
andauthored
Improve type definitions of wrapped Gesture Components (#1394)
Co-authored-by: Jakub Gonet <jgonet@MacBook-Pro-Jakub.local>
1 parent e880ecb commit 9c7c911

File tree

2 files changed

+28
-44
lines changed

2 files changed

+28
-44
lines changed

examples/Example/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ function MainScreen({ navigation }: StackScreenProps<ParamListBase>) {
135135
renderItem={(props) => (
136136
<MainScreenItem
137137
{...props}
138-
onPressItem={({ key }: { key: string }) => navigation.navigate(key)}
138+
onPressItem={({ key }) => navigation.navigate(key)}
139139
/>
140140
)}
141141
renderScrollComponent={(props) => <ScrollView {...props} />}

src/components/GestureComponents.tsx

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import * as React from 'react';
2-
import { PropsWithChildren } from 'react';
2+
import {
3+
PropsWithChildren,
4+
ForwardedRef,
5+
RefAttributes,
6+
ReactElement,
7+
} from 'react';
38
import {
49
ScrollView as RNScrollView,
510
ScrollViewProps as RNScrollViewProps,
@@ -24,63 +29,42 @@ export const ScrollView = createNativeWrapper<
2429
shouldCancelWhenOutside: false,
2530
});
2631
// backward type compatibility with https://github.com/software-mansion/react-native-gesture-handler/blob/db78d3ca7d48e8ba57482d3fe9b0a15aa79d9932/react-native-gesture-handler.d.ts#L440-L457
32+
// include methods of wrapped components by creating an intersection type with the RN component instead of duplicating them.
2733
// eslint-disable-next-line @typescript-eslint/no-redeclare
28-
export type ScrollView = typeof ScrollView & {
29-
scrollTo(
30-
y?: number | { x?: number; y?: number; animated?: boolean },
31-
x?: number,
32-
animated?: boolean
33-
): void;
34-
scrollToEnd(options?: { animated: boolean }): void;
35-
};
34+
export type ScrollView = typeof ScrollView & RNScrollView;
3635

3736
export const Switch = createNativeWrapper<RNSwitchProps>(RNSwitch, {
3837
shouldCancelWhenOutside: false,
3938
shouldActivateOnStart: true,
4039
disallowInterruption: true,
4140
});
4241
// eslint-disable-next-line @typescript-eslint/no-redeclare
43-
export type Switch = typeof Switch;
42+
export type Switch = typeof Switch & RNSwitch;
4443

4544
export const TextInput = createNativeWrapper<RNTextInputProps>(RNTextInput);
4645
// eslint-disable-next-line @typescript-eslint/no-redeclare
47-
export type TextInput = typeof TextInput;
46+
export type TextInput = typeof TextInput & RNTextInput;
4847

4948
export const DrawerLayoutAndroid = createNativeWrapper<
5049
PropsWithChildren<RNDrawerLayoutAndroidProps>
5150
>(RNDrawerLayoutAndroid, { disallowInterruption: true });
52-
// we use literal object since TS gives error when using RN's `positions`
53-
// @ts-ignore FIXME(TS) maybe this should be removed?
54-
DrawerLayoutAndroid.positions = { Left: 'left', Right: 'right' };
5551
// eslint-disable-next-line @typescript-eslint/no-redeclare
56-
export type DrawerLayoutAndroid = typeof DrawerLayoutAndroid;
52+
export type DrawerLayoutAndroid = typeof DrawerLayoutAndroid &
53+
RNDrawerLayoutAndroid;
5754

58-
export const FlatList = React.forwardRef<RNFlatList<any>, RNFlatListProps<any>>(
59-
(props, ref) => (
60-
<RNFlatList
61-
ref={ref}
62-
{...props}
63-
renderScrollComponent={(scrollProps) => <ScrollView {...scrollProps} />}
64-
/>
65-
)
66-
);
55+
export const FlatList = React.forwardRef((props, ref) => (
56+
<RNFlatList
57+
ref={ref}
58+
{...props}
59+
renderScrollComponent={(scrollProps) => <ScrollView {...scrollProps} />}
60+
/>
61+
)) as <ItemT = any>(
62+
props: PropsWithChildren<
63+
RNFlatListProps<ItemT> &
64+
RefAttributes<FlatList<ItemT>> &
65+
NativeViewGestureHandlerProps
66+
>,
67+
ref: ForwardedRef<FlatList<ItemT>>
68+
) => ReactElement | null;
6769
// eslint-disable-next-line @typescript-eslint/no-redeclare
68-
export type FlatList<ItemT> = React.ComponentType<
69-
RNFlatListProps<ItemT> &
70-
NativeViewGestureHandlerProps &
71-
React.RefAttributes<any>
72-
> & {
73-
scrollToEnd: (params?: { animated?: boolean }) => void;
74-
scrollToIndex: (params: {
75-
animated?: boolean;
76-
index: number;
77-
viewOffset?: number;
78-
viewPosition?: number;
79-
}) => void;
80-
scrollToItem: (params: {
81-
animated?: boolean;
82-
item: ItemT;
83-
viewPosition?: number;
84-
}) => void;
85-
scrollToOffset: (params: { animated?: boolean; offset: number }) => void;
86-
};
70+
export type FlatList<ItemT = any> = typeof FlatList & RNFlatList<ItemT>;

0 commit comments

Comments
 (0)