Skip to content

Commit 16f3ac2

Browse files
rubennortefacebook-github-bot
authored andcommitted
Make Lists, Networking, and UIManager modules Flow strict-local (#57726)
Summary: Upgrade the FlatList/SectionList list components, the networking modules (`XMLHttpRequest`, `FormData`, `RCTNetworking`, `convertRequestBody`), and the UIManager modules from `flow` to `flow strict-local`. Public type signatures are preserved exactly, so no consumers need updating; internal `any` and `Object` usages were replaced with accurate types wherever that did not change a public signature. Two public signatures deliberately keep their original loose types behind a scoped `$FlowFixMe[unclear-type]`, because Flow's invariance rules mean any precise annotation would reject existing callers: the `headers` parameters on the `XHRInterceptor` interface (invariant indexers), and `commandArgs` on `UIManager.dispatchViewManagerCommand` (invariant array elements, which would reject callers passing an already-typed array). Changelog: [Internal] Reviewed By: cipolleschi, javache Differential Revision: D113763786
1 parent 8f9232b commit 16f3ac2

16 files changed

Lines changed: 372 additions & 440 deletions

packages/react-native/Libraries/Lists/FlatList.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @flow
7+
* @flow strict-local
88
* @format
99
*/
1010

@@ -78,7 +78,7 @@ type OptionalFlatListProps<ItemT> = {
7878
* any of your `renderItem`, Header, Footer, etc. functions depend on anything outside of the
7979
* `data` prop, stick it here and treat it immutably.
8080
*/
81-
extraData?: any,
81+
extraData?: unknown,
8282
/**
8383
* `getItemLayout` is an optional optimizations that let us skip measurement of dynamic content if
8484
* you know the height of items a priori. `getItemLayout` is the most efficient, and is easy to
@@ -172,9 +172,10 @@ function numColumnsOrDefault(numColumns: ?number) {
172172
return numColumns ?? 1;
173173
}
174174

175-
function isArrayLike(data: unknown): boolean {
176-
// $FlowExpectedError[incompatible-use]
177-
return typeof Object(data).length === 'number';
175+
function isArrayLike<ItemT>(
176+
data: ?Readonly<$ArrayLike<ItemT>>,
177+
): implies data is Readonly<$ArrayLike<ItemT>> {
178+
return data != null && typeof data.length === 'number';
178179
}
179180

180181
type FlatListBaseProps<ItemT> = {
@@ -305,6 +306,7 @@ export type FlatListProps<ItemT> = Readonly<{
305306
*
306307
* Also inherits [ScrollView Props](docs/scrollview.html#props), unless it is nested in another FlatList of same orientation.
307308
*/
309+
// flowlint-next-line unclear-type:off
308310
class FlatList<ItemT = any> extends React.PureComponent<FlatListProps<ItemT>> {
309311
/**
310312
* Scrolls to the end of the content. May be janky without `getItemLayout` prop.
@@ -404,7 +406,7 @@ class FlatList<ItemT = any> extends React.PureComponent<FlatListProps<ItemT>> {
404406
}
405407
}
406408

407-
getScrollableNode(): any {
409+
getScrollableNode(): ?number {
408410
if (this._listRef) {
409411
return this._listRef.getScrollableNode();
410412
}
@@ -499,7 +501,10 @@ class FlatList<ItemT = any> extends React.PureComponent<FlatListProps<ItemT>> {
499501
'FlatList does not support custom data formats.',
500502
);
501503
if (numColumns > 1) {
502-
invariant(!horizontal, 'numColumns does not support horizontal.');
504+
invariant(
505+
Boolean(horizontal) === false,
506+
'numColumns does not support horizontal.',
507+
);
503508
} else {
504509
invariant(
505510
!columnWrapperStyle,
@@ -611,11 +616,13 @@ class FlatList<ItemT = any> extends React.PureComponent<FlatListProps<ItemT>> {
611616
}
612617

613618
_renderer = (
614-
ListItemComponent: ?(React.ComponentType<any> | React.MixedElement),
619+
ListItemComponent: ?(
620+
React.ComponentType<ListRenderItemInfo<ItemT>> | React.MixedElement
621+
),
615622
renderItem: ?ListRenderItem<ItemT>,
616623
columnWrapperStyle: ?ViewStyleProp,
617624
numColumns: ?number,
618-
extraData: ?any,
625+
extraData: ?unknown,
619626
// $FlowFixMe[missing-local-annot]
620627
) => {
621628
const cols = numColumnsOrDefault(numColumns);

packages/react-native/Libraries/Lists/SectionList.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @flow
7+
* @flow strict-local
88
* @format
99
*/
1010

@@ -26,6 +26,7 @@ import * as React from 'react';
2626
const VirtualizedSectionList = VirtualizedLists.VirtualizedSectionList;
2727

2828
type DefaultSectionT = {
29+
// flowlint-next-line unclear-type:off
2930
[key: string]: any,
3031
};
3132

@@ -74,7 +75,7 @@ type OptionalSectionListProps<ItemT, SectionT = DefaultSectionT> = {
7475
* any of your `renderItem`, Header, Footer, etc. functions depend on anything outside of the
7576
* `data` prop, stick it here and treat it immutably.
7677
*/
77-
extraData?: any,
78+
extraData?: unknown,
7879
/**
7980
* How many items to render in the initial batch. This should be enough to fill the screen but not
8081
* much more. Note these items will never be unmounted as part of the windowed rendering in order
@@ -172,6 +173,7 @@ export type SectionListProps<ItemT, SectionT = DefaultSectionT> = {
172173
* @see https://reactnative.dev/docs/sectionlist
173174
*/
174175
export default class SectionList<
176+
// flowlint-next-line unclear-type:off
175177
ItemT = any,
176178
SectionT = DefaultSectionT,
177179
> extends React.PureComponent<SectionListProps<ItemT, SectionT>> {
@@ -226,14 +228,14 @@ export default class SectionList<
226228
/**
227229
* Provides a handle to the underlying scroll node.
228230
*/
229-
getScrollableNode(): any {
231+
getScrollableNode(): ?number {
230232
const listRef = this._wrapperListRef && this._wrapperListRef.getListRef();
231233
if (listRef) {
232234
return listRef.getScrollableNode();
233235
}
234236
}
235237

236-
setNativeProps(props: Object) {
238+
setNativeProps(props: {[string]: unknown, ...}) {
237239
const listRef = this._wrapperListRef && this._wrapperListRef.getListRef();
238240
if (listRef) {
239241
listRef.setNativeProps(props);

packages/react-native/Libraries/Lists/SectionListModern.js

Lines changed: 0 additions & 248 deletions
This file was deleted.

packages/react-native/Libraries/Network/FormData.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type FormDataValue = string | {name?: string, type?: string, uri: string};
1414
type FormDataNameValuePair = [string, FormDataValue];
1515

1616
type Headers = {[name: string]: string, ...};
17-
type FormDataPart =
17+
export type FormDataPart =
1818
| {
1919
string: string,
2020
headers: Headers,

0 commit comments

Comments
 (0)