Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FlatList item generic types #5528

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions __typetests__/common.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,25 @@ function AnimatedFlatListTest() {
/>
);
}

function AnimatedFlatListTestGenericParameterPresence() {
return (
<>
<Animated.FlatList<string> data={['a', 'b']} renderItem={() => null} />
{/* @ts-expect-error Properly detects wrong generic type */}
<Animated.FlatList<string> data={[1, 2]} renderItem={() => null} />
</>
);
}

function AnimatedFlatListTestGenericParameterDefaultsToAny() {
return (
<>
{/* @ts-expect-error Disable TypeScript item type inference */}
<Animated.FlatList renderItem={({ item }) => item.absurdProperty} />
</>
);
}
}

function MakeMutableTest() {
Expand Down
83 changes: 46 additions & 37 deletions src/reanimated2/component/FlatList.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';
import type { ForwardedRef } from 'react';
import React, { forwardRef } from 'react';
import type {
FlatListProps,
Expand Down Expand Up @@ -60,48 +59,58 @@ interface AnimatedFlatListComplement<T> extends FlatList<T> {
getNode(): FlatList<T>;
}

export const ReanimatedFlatList = forwardRef(
(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
props: ReanimatedFlatListPropsWithLayout<any>,
ref: ForwardedRef<FlatList>
) => {
const { itemLayoutAnimation, skipEnteringExitingAnimations, ...restProps } =
props;
// We need explicit any here, because this is the exact same type that is used in React Native types.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const FlatListForwardRefRender = function <Item = any>(
props: ReanimatedFlatListPropsWithLayout<Item>,
ref: React.ForwardedRef<FlatList>
) {
const { itemLayoutAnimation, skipEnteringExitingAnimations, ...restProps } =
props;

// Set default scrollEventThrottle, because user expects
// to have continuous scroll events and
// react-native defaults it to 50 for FlatLists.
// We set it to 1 so we have peace until
// there are 960 fps screens.
if (!('scrollEventThrottle' in restProps)) {
restProps.scrollEventThrottle = 1;
}
// Set default scrollEventThrottle, because user expects
// to have continuous scroll events and
// react-native defaults it to 50 for FlatLists.
// We set it to 1, so we have peace until
// there are 960 fps screens.
if (!('scrollEventThrottle' in restProps)) {
restProps.scrollEventThrottle = 1;
}

const CellRendererComponent = React.useMemo(
() => createCellRendererComponent(itemLayoutAnimation),
[]
);
const CellRendererComponent = React.useMemo(
() => createCellRendererComponent(itemLayoutAnimation),
[]
);

const animatedFlatList = (
<AnimatedFlatList
ref={ref}
{...restProps}
CellRendererComponent={CellRendererComponent}
/>
);
const animatedFlatList = (
// @ts-expect-error In its current type state, createAnimatedComponent cannot create generic components.
<AnimatedFlatList
ref={ref}
{...restProps}
CellRendererComponent={CellRendererComponent}
/>
);

if (skipEnteringExitingAnimations === undefined) {
return animatedFlatList;
}

if (skipEnteringExitingAnimations === undefined) {
return animatedFlatList;
}
return (
<LayoutAnimationConfig skipEntering skipExiting>
{animatedFlatList}
</LayoutAnimationConfig>
);
};

return (
<LayoutAnimationConfig skipEntering skipExiting>
{animatedFlatList}
</LayoutAnimationConfig>
);
export const ReanimatedFlatList = forwardRef(FlatListForwardRefRender) as <
// We need explicit any here, because this is the exact same type that is used in React Native types.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
ItemT = any
>(
props: ReanimatedFlatListPropsWithLayout<ItemT> & {
ref?: React.ForwardedRef<FlatList>;
}
);
) => React.ReactElement;
tjzel marked this conversation as resolved.
Show resolved Hide resolved

export type ReanimatedFlatList<T> = typeof AnimatedFlatList &
AnimatedFlatListComplement<T>;