Skip to content

Commit

Permalink
VirtualizedList/FlatList: Stricter typing for onViewableItemsChanged (#…
Browse files Browse the repository at this point in the history
…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: #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
  • Loading branch information
miallo authored and facebook-github-bot committed Feb 1, 2024
1 parent 07e8ae4 commit 35f5c3a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/react-native/Libraries/Lists/FlatList.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ export interface FlatListProps<ItemT> extends VirtualizedListProps<ItemT> {
*/
onViewableItemsChanged?:
| ((info: {
viewableItems: Array<ViewToken>;
changed: Array<ViewToken>;
viewableItems: Array<ViewToken<ItemT>>;
changed: Array<ViewToken<ItemT>>;
}) => void)
| null
| undefined;
Expand Down
8 changes: 4 additions & 4 deletions packages/virtualized-lists/Lists/VirtualizedList.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import type {
ScrollView,
} from 'react-native';

export interface ViewToken {
item: any;
export interface ViewToken<ItemT = any> {
item: ItemT;
key: string;
index: number | null;
isViewable: boolean;
Expand Down Expand Up @@ -330,8 +330,8 @@ export interface VirtualizedListWithoutRenderItemProps<ItemT>
*/
onViewableItemsChanged?:
| ((info: {
viewableItems: Array<ViewToken>;
changed: Array<ViewToken>;
viewableItems: Array<ViewToken<ItemT>>;
changed: Array<ViewToken<ItemT>>;
}) => void)
| null
| undefined;
Expand Down

0 comments on commit 35f5c3a

Please sign in to comment.