From 54bf3ae1cfaf49562d60ebf06386c157fc27131e Mon Sep 17 00:00:00 2001 From: Eli White Date: Wed, 28 Aug 2019 13:14:00 -0700 Subject: [PATCH] Improve Flow Type for ScrollResponder Summary: FlatList and VirtualizedList were typing this value as any instead of using the actual type from ScrollView. I started with that change and then fixed the type to solve the other callsites in the codebase. Reviewed By: zackargyle Differential Revision: D17089934 fbshipit-source-id: bfc22cec9993904d779cad37b1de7cb3c0484d2c --- Libraries/Components/ScrollView/ScrollView.js | 16 +++++++++++++++- Libraries/Lists/FlatList.js | 3 ++- Libraries/Lists/SectionList.js | 3 ++- Libraries/Lists/VirtualizedList.js | 3 ++- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/Libraries/Components/ScrollView/ScrollView.js b/Libraries/Components/ScrollView/ScrollView.js index b4298158b3354f..b15584381de031 100644 --- a/Libraries/Components/ScrollView/ScrollView.js +++ b/Libraries/Components/ScrollView/ScrollView.js @@ -64,7 +64,21 @@ if (Platform.OS === 'android') { } export type ScrollResponderType = { - ...ScrollView, + // We'd like to do ...ScrollView here, however Flow doesn't seem + // to see the imperative methods of ScrollView that way. Workaround the + // issue by specifying them manually. + getScrollableNode: $PropertyType, + getInnerViewNode: $PropertyType, + getNativeScrollRef: $PropertyType, + + setNativeProps: $PropertyType, + scrollTo: $PropertyType, + scrollWithoutAnimationTo: $PropertyType< + ScrollView, + 'scrollWithoutAnimationTo', + >, + flashScrollIndicators: $PropertyType, + ...typeof ScrollResponder.Mixin, }; diff --git a/Libraries/Lists/FlatList.js b/Libraries/Lists/FlatList.js index 3afed1ba5a75b7..41354f7e53ceb1 100644 --- a/Libraries/Lists/FlatList.js +++ b/Libraries/Lists/FlatList.js @@ -18,6 +18,7 @@ const StyleSheet = require('../StyleSheet/StyleSheet'); const invariant = require('invariant'); +import type {ScrollResponderType} from '../Components/ScrollView/ScrollView'; import type {ViewStyleProp} from '../StyleSheet/StyleSheet'; import type { ViewabilityConfig, @@ -441,7 +442,7 @@ class FlatList extends React.PureComponent, void> { /** * Provides a handle to the underlying scroll responder. */ - getScrollResponder(): any { + getScrollResponder(): ?ScrollResponderType { if (this._listRef) { return this._listRef.getScrollResponder(); } diff --git a/Libraries/Lists/SectionList.js b/Libraries/Lists/SectionList.js index 99b621a2eb350f..d9ef718090a7e3 100644 --- a/Libraries/Lists/SectionList.js +++ b/Libraries/Lists/SectionList.js @@ -14,6 +14,7 @@ const React = require('react'); const ScrollView = require('../Components/ScrollView/ScrollView'); const VirtualizedSectionList = require('./VirtualizedSectionList'); +import type {ScrollResponderType} from '../Components/ScrollView/ScrollView'; import type {ViewToken} from './ViewabilityHelper'; import type { SectionBase as _SectionBase, @@ -275,7 +276,7 @@ class SectionList> extends React.PureComponent< /** * Provides a handle to the underlying scroll responder. */ - getScrollResponder(): ?ScrollView { + getScrollResponder(): ?ScrollResponderType { const listRef = this._wrapperListRef && this._wrapperListRef.getListRef(); if (listRef) { return listRef.getScrollResponder(); diff --git a/Libraries/Lists/VirtualizedList.js b/Libraries/Lists/VirtualizedList.js index 7dffc1729ef26c..62f614ec06e497 100644 --- a/Libraries/Lists/VirtualizedList.js +++ b/Libraries/Lists/VirtualizedList.js @@ -27,6 +27,7 @@ const warning = require('fbjs/lib/warning'); const {computeWindowedRenderLimits} = require('./VirtualizeUtils'); +import type {ScrollResponderType} from '../Components/ScrollView/ScrollView'; import type {ViewStyleProp} from '../StyleSheet/StyleSheet'; import type { ViewabilityConfig, @@ -431,7 +432,7 @@ class VirtualizedList extends React.PureComponent { * Note that `this._scrollRef` might not be a `ScrollView`, so we * need to check that it responds to `getScrollResponder` before calling it. */ - getScrollResponder(): any { + getScrollResponder(): ?ScrollResponderType { if (this._scrollRef && this._scrollRef.getScrollResponder) { return this._scrollRef.getScrollResponder(); }