From 35f5c3a204375c2e716cdaa624f6c931ad62520e Mon Sep 17 00:00:00 2001 From: Michael Lohmann Date: Thu, 1 Feb 2024 15:25:10 -0800 Subject: [PATCH] VirtualizedList/FlatList: Stricter typing for onViewableItemsChanged (#42773) Summary: The previous typing of FlatList and VirtualizedList did not convey any information on the type of the items passed in to `onViewableItemsChanged`, but instead the type was set to `any`. This PR adds the type information. I set a default type `any` for thy ViewToken, because the type is exported and not having it would be a breaking change if that type is used. Like this it gracefully falls back to the default behavior of the `any` type. Notice: I don't know how typing in "flow" works, but the same "issue" seems to be in there as well. Maybe someone with more flow experience can fix that as well: https://github.com/facebook/react-native/blob/ae42e0202de2c3db489caf63839fced7b52efc5d/packages/virtualized-lists/Lists/ViewabilityHelper.js#L19-L20 ## Changelog: [GENERAL] [FIXED] - Add type information for items of VirtualizedList in `onViewableItemsChanged` signature [GENERAL] [FIXED] - Add type information for items of FlatList in `onViewableItemsChanged` signature Pull Request resolved: https://github.com/facebook/react-native/pull/42773 Test Plan: Without the changes, typecheck of the project was fine, but with the changes applied to the node_modules/react-native copy a type error was found: ``` $ npm run typecheck > my-project@1.0.0 typecheck > tsc --skipLibCheck src/MyComponent.tsx:385:29 - error TS2345: Argument of type '(string | number)[]' is not assignable to parameter of type 'number[]'. Type 'string | number' is not assignable to type 'number'. Type 'string' is not assignable to type 'number'. 385 viewableItems ~~~~~~~~~~~~~ 386 .filter( ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ``` Reviewed By: rozele Differential Revision: D53276749 Pulled By: NickGerleman fbshipit-source-id: 3fa5c65b388a59942c106286ac502a85c583da50 --- packages/react-native/Libraries/Lists/FlatList.d.ts | 4 ++-- packages/virtualized-lists/Lists/VirtualizedList.d.ts | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/react-native/Libraries/Lists/FlatList.d.ts b/packages/react-native/Libraries/Lists/FlatList.d.ts index 5bbe2fa3b80c7a..cdf51e25ae2d0c 100644 --- a/packages/react-native/Libraries/Lists/FlatList.d.ts +++ b/packages/react-native/Libraries/Lists/FlatList.d.ts @@ -116,8 +116,8 @@ export interface FlatListProps extends VirtualizedListProps { */ onViewableItemsChanged?: | ((info: { - viewableItems: Array; - changed: Array; + viewableItems: Array>; + changed: Array>; }) => void) | null | undefined; diff --git a/packages/virtualized-lists/Lists/VirtualizedList.d.ts b/packages/virtualized-lists/Lists/VirtualizedList.d.ts index a2740604155767..a4c93f58a266b5 100644 --- a/packages/virtualized-lists/Lists/VirtualizedList.d.ts +++ b/packages/virtualized-lists/Lists/VirtualizedList.d.ts @@ -18,8 +18,8 @@ import type { ScrollView, } from 'react-native'; -export interface ViewToken { - item: any; +export interface ViewToken { + item: ItemT; key: string; index: number | null; isViewable: boolean; @@ -330,8 +330,8 @@ export interface VirtualizedListWithoutRenderItemProps */ onViewableItemsChanged?: | ((info: { - viewableItems: Array; - changed: Array; + viewableItems: Array>; + changed: Array>; }) => void) | null | undefined;