Skip to content

Commit f0d6ab6

Browse files
huntiemeta-codesync[bot]
authored andcommitted
Add *Instance type exports for all built-in components
Summary: Add a publicly exported `*Instance` type for every built-in component in `index.js.flow`, giving consumers a consistent way to type refs for any React Native component. Affects the Strict TypeScript API (opt-in generated TS types) only. #### Motivation + RFC Draft implementation for the recommended approach in react-native-community/discussions-and-proposals#1003. Alternative to facebook#56673. #### New types | Component | Instance Type | Type Value | |---|---|---| | `ActivityIndicator` | `ActivityIndicatorInstance` | `HostInstance` | | `Button` | `ButtonInstance` | `ButtonRef` (platform-dependent) | | `DrawerLayoutAndroid` | `DrawerLayoutAndroidInstance` | `DrawerLayoutAndroidMethods` | | `FlatList` | `FlatListInstance` | `FlatList<>` | | `Image` | `ImageInstance` | `HostInstance` | | `ImageBackground` | `ImageBackgroundInstance` | `ImageBackground` (class) | | `KeyboardAvoidingView` | `KeyboardAvoidingViewInstance` | `KeyboardAvoidingView` (class) | | `Modal` | `ModalInstance` | `HostInstance` | | `Pressable` | `PressableInstance` | `HostInstance` | | `ProgressBarAndroid` | `ProgressBarAndroidInstance` | `HostInstance` | | `RefreshControl` | `RefreshControlInstance` | `RefreshControl` (class) | | `SafeAreaView` | `SafeAreaViewInstance` | `HostInstance` | | `ScrollView` | `ScrollViewInstance` | `extends HostInstance, ScrollViewImperativeMethods` | | `SectionList` | `SectionListInstance` | `SectionList<>` | | `StatusBar` | `StatusBarInstance` | `StatusBar` (class) | | `Switch` | `SwitchInstance` | `HostInstance` | | `Text` | `TextInstance` | `HostInstance` | | `TextInput` | `TextInputInstance` | `extends ReactNativeElement` (existing) | | `TouchableHighlight` | `TouchableHighlightInstance` | `HostInstance` | | `TouchableNativeFeedback` | `TouchableNativeFeedbackInstance` | `TouchableNativeFeedback` (class) | | `TouchableOpacity` | `TouchableOpacityInstance` | `HostInstance` | | `View` | `ViewInstance` | `HostInstance` | | `VirtualizedList` | `VirtualizedListInstance` | `React.ElementRef<VirtualizedListType>` | | `VirtualizedSectionList` | `VirtualizedSectionListInstance` | `React.ElementRef<VirtualizedSectionListType>` | #### Renames - `PublicScrollViewInstance` → `ScrollViewInstance` (old name kept as deprecated alias; internal usages in `ScrollView.js` and `FlatList.js` updated) - `PublicModalInstance` → `ModalInstance` (old name kept as deprecated alias) #### Skipped (no ref support) `InputAccessoryView`, `experimental_LayoutConformance`, `TouchableWithoutFeedback`, `Touchable` (namespace) Changelog: [General][Added] - **Strict TypeScript API**: Add `*Instance` ref types for all built-in components Differential Revision: D106144383
1 parent cdb483c commit f0d6ab6

32 files changed

Lines changed: 232 additions & 81 deletions

packages/react-native/Libraries/Components/ActivityIndicator/ActivityIndicator.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@
1010

1111
'use strict';
1212
import type {HostComponent} from '../../../src/private/types/HostComponent';
13+
import type {HostInstance} from '../../../src/private/types/HostInstance';
1314
import type {ViewProps} from '../View/ViewPropTypes';
1415

1516
import StyleSheet, {type ColorValue} from '../../StyleSheet/StyleSheet';
1617
import Platform from '../../Utilities/Platform';
1718
import View from '../View/View';
1819
import * as React from 'react';
1920

21+
export type ActivityIndicatorInstance = HostInstance;
22+
2023
const PlatformActivityIndicator =
2124
Platform.OS === 'android'
2225
? require('../ProgressBarAndroid/ProgressBarAndroid').default

packages/react-native/Libraries/Components/Button.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ const NativeTouchable:
286286

287287
type ButtonRef = React.ElementRef<typeof NativeTouchable>;
288288

289+
export type ButtonInstance = ButtonRef;
290+
289291
const Button: component(
290292
ref?: React.RefSetter<ButtonRef>,
291293
...props: ButtonProps

packages/react-native/Libraries/Components/DrawerAndroid/DrawerLayoutAndroid.android.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,6 @@ const styles = StyleSheet.create({
302302
},
303303
});
304304

305+
export type DrawerLayoutAndroidInstance = DrawerLayoutAndroid;
306+
305307
export default DrawerLayoutAndroid as $FlowFixMe;

packages/react-native/Libraries/Components/DrawerAndroid/DrawerLayoutAndroid.ios.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@
1212

1313
import DrawerLayoutAndroidFallback from './DrawerLayoutAndroidFallback';
1414

15+
export type DrawerLayoutAndroidInstance = DrawerLayoutAndroidFallback;
16+
1517
export default DrawerLayoutAndroidFallback;

packages/react-native/Libraries/Components/DrawerAndroid/DrawerLayoutAndroidTypes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,5 @@ export interface DrawerLayoutAndroidMethods {
136136
): void;
137137
setNativeProps(nativeProps: Object): void;
138138
}
139+
140+
export type DrawerLayoutAndroidInstance = DrawerLayoutAndroidMethods;

packages/react-native/Libraries/Components/Keyboard/KeyboardAvoidingView.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,4 +298,6 @@ class KeyboardAvoidingView extends React.Component<
298298
}
299299
}
300300

301+
export type KeyboardAvoidingViewInstance = KeyboardAvoidingView;
302+
301303
export default KeyboardAvoidingView;

packages/react-native/Libraries/Components/Pressable/Pressable.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* @format
99
*/
1010

11+
import type {HostInstance} from '../../../src/private/types/HostInstance';
1112
import type {ViewStyleProp} from '../../StyleSheet/StyleSheet';
1213
import type {
1314
GestureResponderEvent,
@@ -27,6 +28,8 @@ import useAndroidRippleForView, {
2728
import * as React from 'react';
2829
import {memo, useMemo, useRef, useState} from 'react';
2930

31+
export type PressableInstance = HostInstance;
32+
3033
export type {PressableAndroidRippleConfig};
3134

3235
export type PressableStateCallbackType = Readonly<{

packages/react-native/Libraries/Components/ProgressBarAndroid/ProgressBarAndroid.android.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88
* @format
99
*/
1010

11+
import type {HostInstance} from '../../../src/private/types/HostInstance';
1112
import type {ProgressBarAndroidProps} from './ProgressBarAndroidTypes';
1213

1314
import ProgressBarAndroidNativeComponent from './ProgressBarAndroidNativeComponent';
1415

1516
const React = require('react');
1617

18+
export type ProgressBarAndroidInstance = HostInstance;
19+
1720
export type {ProgressBarAndroidProps};
1821

1922
/**

packages/react-native/Libraries/Components/ProgressBarAndroid/ProgressBarAndroid.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010

1111
'use strict';
1212

13+
import type {HostInstance} from '../../../src/private/types/HostInstance';
1314
import typeof ProgressBarAndroidNativeComponentType from './ProgressBarAndroidNativeComponent';
1415
import type {ProgressBarAndroidProps} from './ProgressBarAndroidTypes';
1516

1617
import Platform from '../../Utilities/Platform';
1718

19+
export type ProgressBarAndroidInstance = HostInstance;
20+
1821
export type {ProgressBarAndroidProps};
1922

2023
// A utility type to preserve the semantics of the union uses in the definition

packages/react-native/Libraries/Components/RefreshControl/RefreshControl.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,6 @@ class RefreshControl extends React.Component<RefreshControlProps> {
205205
};
206206
}
207207

208+
export type RefreshControlInstance = RefreshControl;
209+
208210
export default RefreshControl;

0 commit comments

Comments
 (0)