From ed39d639ea196181732186df735f5e58543ace32 Mon Sep 17 00:00:00 2001 From: Jerry Date: Tue, 28 Feb 2023 15:17:21 -0800 Subject: [PATCH] Fix/animated lists types (#36292) Summary: I was working on `Animated.FlatList` and found some types missing as below ![Screen Shot 2023-02-25 at 4 32 43 PM](https://user-images.githubusercontent.com/64301935/221345457-74252131-5207-4e17-ad96-92221a915305.png) also `Animated.SectionList` ![Screen Shot 2023-02-25 at 4 31 34 PM](https://user-images.githubusercontent.com/64301935/221345679-07ba862b-708e-400e-ac14-7d2156fcc1e8.png) So I refactored type definition for `Animated.FlatList` and `Animated.SectionList` using `abstract class`. ## Changelog [GENERAL] [FIXED] - add missing FlatList types for Animated FlatList [GENERAL] [FIXED] - add missing SectionList types for Animated SectionList Pull Request resolved: https://github.com/facebook/react-native/pull/36292 Test Plan: Ran `yarn test-typescript` and `yarn test-typescript-offline` with no errors. Reviewed By: lunaleaps Differential Revision: D43673884 Pulled By: NickGerleman fbshipit-source-id: 7ccab5997fa2f22226fb0e106672cee98e568ba4 --- Libraries/Animated/Animated.d.ts | 17 +++++++++++++---- Libraries/Lists/FlatList.d.ts | 12 +++++++++--- Libraries/Lists/SectionList.d.ts | 12 ++++++++---- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/Libraries/Animated/Animated.d.ts b/Libraries/Animated/Animated.d.ts index 3301ff377246ff..fb2aeafc5eaca7 100644 --- a/Libraries/Animated/Animated.d.ts +++ b/Libraries/Animated/Animated.d.ts @@ -11,8 +11,12 @@ import type * as React from 'react'; import {ScrollView} from '../Components/ScrollView/ScrollView'; import {View} from '../Components/View/View'; import {Image} from '../Image/Image'; -import {FlatListProps} from '../Lists/FlatList'; -import {DefaultSectionT, SectionListProps} from '../Lists/SectionList'; +import {FlatListComponent, FlatListProps} from '../Lists/FlatList'; +import { + DefaultSectionT, + SectionListComponent, + SectionListProps, +} from '../Lists/SectionList'; import {ColorValue} from '../StyleSheet/StyleSheet'; import {Text} from '../Text/Text'; import {NativeSyntheticEvent} from '../Types/CoreEventTypes'; @@ -598,13 +602,18 @@ export namespace Animated { /** * FlatList and SectionList infer generic Type defined under their `data` and `section` props. */ - export class FlatList extends React.Component< + + export class FlatList extends FlatListComponent< + ItemT, AnimatedProps> > {} + export class SectionList< ItemT = any, SectionT = DefaultSectionT, - > extends React.Component>> {} + > extends SectionListComponent< + AnimatedProps> + > {} } // We need to alias these views so we can reference them in the Animated diff --git a/Libraries/Lists/FlatList.d.ts b/Libraries/Lists/FlatList.d.ts index 8676fb4d28b4f8..d3e350c01971a7 100644 --- a/Libraries/Lists/FlatList.d.ts +++ b/Libraries/Lists/FlatList.d.ts @@ -166,9 +166,10 @@ export interface FlatListProps extends VirtualizedListProps { fadingEdgeLength?: number | undefined; } -export class FlatList extends React.Component< - FlatListProps -> { +export abstract class FlatListComponent< + ItemT, + Props, +> extends React.Component { /** * Scrolls to the end of the content. May be janky without `getItemLayout` prop. */ @@ -236,3 +237,8 @@ export class FlatList extends React.Component< // TODO: use `unknown` instead of `any` for Typescript >= 3.0 setNativeProps: (props: {[key: string]: any}) => void; } + +export class FlatList extends FlatListComponent< + ItemT, + FlatListProps +> {} diff --git a/Libraries/Lists/SectionList.d.ts b/Libraries/Lists/SectionList.d.ts index 7ff5bbb0a3cbe1..0e2313702d6cca 100644 --- a/Libraries/Lists/SectionList.d.ts +++ b/Libraries/Lists/SectionList.d.ts @@ -210,10 +210,9 @@ export interface SectionListScrollParams { viewPosition?: number | undefined; } -export class SectionList< - ItemT = any, - SectionT = DefaultSectionT, -> extends React.Component> { +export abstract class SectionListComponent< + Props, +> extends React.Component { /** * Scrolls to the item at the specified sectionIndex and itemIndex (within the section) * positioned in the viewable area such that viewPosition 0 places it at the top @@ -246,6 +245,11 @@ export class SectionList< getScrollableNode(): NodeHandle | undefined; } +export class SectionList< + ItemT = any, + SectionT = DefaultSectionT, +> extends SectionListComponent> {} + /* This definition is deprecated because it extends the wrong base type */ export interface SectionListStatic extends React.ComponentClass> {