react-native-snap-carousel
react-native-snap-carousel copied to clipboard
renderItem typescript error with item unknown
Really love this snap carousel. I'm having an issue where I get TypeScript error on renderItem.
import MealCarousel from 'react-native-snap-carousel';
const RestaurantListRow: React.FC<Restaurant> = React.memo(
(item) => {
const {
...
meals,
} = item;
const renderItem = React.useCallback(
({ item, index }: { item: Meal; index: number }) => {
return (
<TouchableWithoutFeedback
onPress={() => ...}>
<RestaurantMeal meal={item} />
</TouchableWithoutFeedback>
);
},
[meals, ...]
);
return(
...
<MealCarousel
data={meals}
renderItem={renderItem}
sliderWidth={width}
itemWidth={width - 30}
removeClippedSubviews={false}
/>
...
I am importing meals, which is a Meal[] that cannot contain unknown objects.
When I hover on top of item in the render function, I can see the item is of type Meal.
The full error I am getting is:
Type '({ item }: { item: Meal; }) => JSX.Element' is not assignable to type '((baseData: { index: number; dataIndex: number; item: unknown; }, parallaxData: { scrollPosition: Value | undefined; carouselRef: ScrollView | FlatList<unknown> | null; vertical: false; itemWidth: number; sliderWidth: number; }) => ReactNode) | ((baseData: { ...; }, parallaxData: { ...; }) => ReactNode)'.
Type '({ item }: { item: Meal; }) => JSX.Element' is not assignable to type '(baseData: { index: number; dataIndex: number; item: unknown; }, parallaxData: { scrollPosition: Value | undefined; carouselRef: ScrollView | FlatList<unknown> | null; vertical: false; itemWidth: number; sliderWidth: number; }) => ReactNode'.
Types of parameters '__0' and 'baseData' are incompatible.
Type '{ index: number; dataIndex: number; item: unknown; }' is not assignable to type '{ item: Meal; }'.
Types of property 'item' are incompatible.
However, if I simple render a snap carousel using an simple array of items from a local file, then I don't get this error.
What am I doing wrong?
I should also mention that when I run tsc I am getting an error for the typescript definition file of the library as well and for renderItem as well:
node_modules/react-native-snap-carousel/lib/typescript/carousel/types.d.ts:58:5
58 renderItem: (baseData: {
~~~~~~~~~~
The expected type comes from property 'renderItem' which is declared here on type '(IntrinsicAttributes & IntrinsicClassAttributes<Carousel<unknown>> & Pick<Readonly<CarouselBaseProps<unknown> & HorizontalCarouselProps<...> & Pick<...>> & Readonly<...>, "data" | ... 18 more ... | "renderItem"> & Partial<...> & Partial<...>) | (IntrinsicAttributes & ... 3 more ... & Partial<...>)'
Hey ! Check this : https://github.com/archriss/react-native-snap-carousel/pull/678#issuecomment-693518137
Sorry, please allow me to advertise for my open source library! ~ I think this library react-native-reanimated-carousel will solve your problem. It is a high performance and very simple component, complete with React-Native reanimated 2
Having same issues.
Still having issues in 2024
The Documentation needs to use Typecript and functional component.