Skip to content

Commit c514110

Browse files
committed
Allow FlatList to take generics.
This allows both the previous hack of: const StringFlatList = FlatList as { new(): FlatList<string> }; As well as should support upcoming generics in TSX: microsoft/TypeScript#6395 E.g. <FlatList<string> data=... renderItem=... />
1 parent 8177745 commit c514110

File tree

2 files changed

+46
-46
lines changed

2 files changed

+46
-46
lines changed

types/react-native/index.d.ts

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
//
2323
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2424

25-
/// <reference types="react" />
26-
2725
/// <reference path="globals.d.ts" />
2826

27+
import * as React from 'react';
28+
2929
export type MeasureOnSuccessCallback = (
3030
x: number,
3131
y: number,
@@ -3635,48 +3635,6 @@ export interface FlatListProperties<ItemT> extends VirtualizedListProperties<Ite
36353635
removeClippedSubviews?: boolean;
36363636
}
36373637

3638-
export interface FlatListStatic<ItemT> extends React.ComponentClass<FlatListProperties<ItemT>> {
3639-
/**
3640-
* Exports some data, e.g. for perf investigations or analytics.
3641-
*/
3642-
getMetrics: () => {
3643-
contentLength: number;
3644-
totalRows: number;
3645-
renderedRows: number;
3646-
visibleRows: number;
3647-
};
3648-
3649-
/**
3650-
* Scrolls to the end of the content. May be janky without `getItemLayout` prop.
3651-
*/
3652-
scrollToEnd: (params?: { animated?: boolean }) => void;
3653-
3654-
/**
3655-
* Scrolls to the item at the specified index such that it is positioned in the viewable area
3656-
* such that viewPosition 0 places it at the top, 1 at the bottom, and 0.5 centered in the middle.
3657-
* Cannot scroll to locations outside the render window without specifying the getItemLayout prop.
3658-
*/
3659-
scrollToIndex: (params: { animated?: boolean; index: number; viewOffset?: number; viewPosition?: number }) => void;
3660-
3661-
/**
3662-
* Requires linear scan through data - use `scrollToIndex` instead if possible.
3663-
* May be janky without `getItemLayout` prop.
3664-
*/
3665-
scrollToItem: (params: { animated?: boolean; item: ItemT; viewPosition?: number }) => void;
3666-
3667-
/**
3668-
* Scroll to a specific content pixel offset, like a normal `ScrollView`.
3669-
*/
3670-
scrollToOffset: (params: { animated?: boolean; offset: number }) => void;
3671-
3672-
/**
3673-
* Tells the list an interaction has occured, which should trigger viewability calculations,
3674-
* e.g. if waitForInteractions is true and the user has not scrolled. This is typically called
3675-
* by taps on items or by navigation actions.
3676-
*/
3677-
recordInteraction: () => void;
3678-
}
3679-
36803638
/**
36813639
* @see https://facebook.github.io/react-native/docs/sectionlist.html
36823640
*/
@@ -8370,8 +8328,47 @@ export type ImageBackground = ImageBackgroundStatic;
83708328
export var ImagePickerIOS: ImagePickerIOSStatic;
83718329
export type ImagePickerIOS = ImagePickerIOSStatic;
83728330

8373-
export var FlatList: FlatListStatic<any>;
8374-
export type FlatList<ItemT> = FlatListStatic<ItemT>;
8331+
export class FlatList<ItemT> extends React.Component<FlatListProperties<ItemT>> {
8332+
/**
8333+
* Exports some data, e.g. for perf investigations or analytics.
8334+
*/
8335+
getMetrics(): {
8336+
contentLength: number;
8337+
totalRows: number;
8338+
renderedRows: number;
8339+
visibleRows: number;
8340+
};
8341+
8342+
/**
8343+
* Scrolls to the end of the content. May be janky without `getItemLayout` prop.
8344+
*/
8345+
scrollToEnd(params?: { animated?: boolean }): void;
8346+
8347+
/**
8348+
* Scrolls to the item at the specified index such that it is positioned in the viewable area
8349+
* such that viewPosition 0 places it at the top, 1 at the bottom, and 0.5 centered in the middle.
8350+
* Cannot scroll to locations outside the render window without specifying the getItemLayout prop.
8351+
*/
8352+
scrollToIndex(params: { animated?: boolean; index: number; viewOffset?: number; viewPosition?: number }): void;
8353+
8354+
/**
8355+
* Requires linear scan through data - use `scrollToIndex` instead if possible.
8356+
* May be janky without `getItemLayout` prop.
8357+
*/
8358+
scrollToItem(params: { animated?: boolean; item: ItemT; viewPosition?: number }): void;
8359+
8360+
/**
8361+
* Scroll to a specific content pixel offset, like a normal `ScrollView`.
8362+
*/
8363+
scrollToOffset(params: { animated?: boolean; offset: number }): void;
8364+
8365+
/**
8366+
* Tells the list an interaction has occured, which should trigger viewability calculations,
8367+
* e.g. if waitForInteractions is true and the user has not scrolled. This is typically called
8368+
* by taps on items or by navigation actions.
8369+
*/
8370+
recordInteraction(): void;
8371+
}
83758372

83768373
export var LayoutAnimation: LayoutAnimationStatic;
83778374
export type LayoutAnimation = LayoutAnimationStatic;

types/react-native/test/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ export class FlatListTest extends React.Component<FlatListProperties<number>, {}
239239
}
240240
}
241241

242+
const StringFlatList = FlatList as { new(): FlatList<string> };
243+
<StringFlatList data={["a", "b"]} renderItem={({ item }) => <Text>{item.toUpperCase()}</Text>} />
244+
242245
export class SectionListTest extends React.Component<SectionListProperties<string>, {}> {
243246
render() {
244247
const sections = [

0 commit comments

Comments
 (0)