Skip to content

Commit a65744f

Browse files
j-piaseckifacebook-github-bot
authored andcommitted
Unify ref usages in TS type tests (react#51117)
Summary: Pull Request resolved: react#51117 Changelog: [Internal] Reviewed By: huntie Differential Revision: D74240080 fbshipit-source-id: 5cc7042b5ac1515887d20b72a0b7d96cc8313ed7
1 parent ef6f2fb commit a65744f

2 files changed

Lines changed: 19 additions & 15 deletions

File tree

packages/react-native/types/__typetests__/animated.tsx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ class Comp extends React.Component<CompProps> {
3131
}
3232
}
3333

34-
const ForwardComp = React.forwardRef<View, CompProps>(({width}, ref) => {
34+
const ForwardComp = React.forwardRef<
35+
React.ComponentRef<typeof View>,
36+
CompProps
37+
>(({width}, ref) => {
3538
function f1(): boolean {
3639
return true;
3740
}
@@ -53,7 +56,7 @@ function TestAnimatedAPI() {
5356
const v2 = new Animated.Value(0);
5457

5558
// Ref
56-
const AnimatedViewRef = React.useRef<View>(null);
59+
const AnimatedViewRef = React.useRef<React.ComponentRef<typeof View>>(null);
5760

5861
AnimatedViewRef.current &&
5962
AnimatedViewRef.current.measure(() => {
@@ -62,15 +65,15 @@ function TestAnimatedAPI() {
6265

6366
const AnimatedComp = Animated.createAnimatedComponent(Comp);
6467

65-
const AnimatedCompRef = React.useRef<Comp>(null);
68+
const AnimatedCompRef = React.useRef<React.ComponentRef<typeof Comp>>(null);
6669

6770
AnimatedCompRef.current && AnimatedCompRef.current.f1();
6871

6972
const AnimatedForwardComp = Animated.createAnimatedComponent(ForwardComp);
7073

7174
const AnimatedForwardCompRef =
72-
React.useRef<React.ElementRef<typeof ForwardComp>>(null);
73-
const ForwardCompRef = React.useRef<View>(null);
75+
React.useRef<React.ComponentRef<typeof ForwardComp>>(null);
76+
const ForwardCompRef = React.useRef<React.ComponentRef<typeof View>>(null);
7477

7578
AnimatedForwardCompRef.current &&
7679
AnimatedForwardCompRef.current.measure(() => {
@@ -168,8 +171,9 @@ function TestAnimatedAPI() {
168171
});
169172

170173
const AnimatedView = Animated.createAnimatedComponent(View);
171-
const ref = React.useRef<View>(null);
172-
const legacyRef = React.useRef<Animated.LegacyRef<View>>(null);
174+
const ref = React.useRef<React.ComponentRef<typeof View>>(null);
175+
const legacyRef =
176+
React.useRef<Animated.LegacyRef<React.ComponentRef<typeof View>>>(null);
173177

174178
return (
175179
<View ref={ref}>

packages/react-native/types/__typetests__/index.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,8 @@ class CustomView extends React.Component {
429429
class Welcome extends React.Component<
430430
ElementProps<View> & {color: string; bgColor?: null | undefined | string}
431431
> {
432-
rootViewRef = React.createRef<View>();
433-
customViewRef = React.createRef<CustomView>();
432+
rootViewRef = React.createRef<React.ComponentRef<typeof View>>();
433+
customViewRef = React.createRef<React.ComponentRef<typeof CustomView>>();
434434

435435
testNativeMethods() {
436436
if (this.rootViewRef.current != null) {
@@ -498,7 +498,7 @@ function TouchableTest() {
498498
}
499499

500500
export class TouchableHighlightTest extends React.Component {
501-
buttonRef = React.createRef<React.ElementRef<typeof TouchableHighlight>>();
501+
buttonRef = React.createRef<React.ComponentRef<typeof TouchableHighlight>>();
502502

503503
render() {
504504
return (
@@ -525,7 +525,7 @@ export class TouchableHighlightTest extends React.Component {
525525
}
526526

527527
export class TouchableOpacityTest extends React.Component {
528-
buttonRef = React.createRef<React.ElementRef<typeof TouchableOpacity>>();
528+
buttonRef = React.createRef<React.ComponentRef<typeof TouchableOpacity>>();
529529

530530
render() {
531531
return (
@@ -720,7 +720,7 @@ export class PressableTest extends React.Component<{}> {
720720
}
721721

722722
// App State
723-
function appStateListener(state: string) {
723+
function appStateListener(state?: string) {
724724
console.log('New state: ' + state);
725725
}
726726

@@ -1575,7 +1575,7 @@ class BridgedComponentTest extends React.Component {
15751575
}
15761576

15771577
const SafeAreaViewTest = () => {
1578-
const viewRef = React.createRef<React.ElementRef<typeof View>>();
1578+
const viewRef = React.createRef<React.ComponentRef<typeof View>>();
15791579

15801580
return (
15811581
<>
@@ -1600,7 +1600,7 @@ const SafeAreaViewTest = () => {
16001600
};
16011601

16021602
const SwitchRefTest = () => {
1603-
const switchRef = React.createRef<React.ElementRef<typeof Switch>>();
1603+
const switchRef = React.createRef<React.ComponentRef<typeof Switch>>();
16041604

16051605
return (
16061606
<>
@@ -2103,7 +2103,7 @@ const AccessibilityCustomActionsTest = () => {
21032103

21042104
// DrawerLayoutAndroidTest
21052105
export class DrawerLayoutAndroidTest extends React.Component {
2106-
drawerRef = React.createRef<DrawerLayoutAndroid>();
2106+
drawerRef = React.createRef<React.ComponentRef<typeof DrawerLayoutAndroid>>();
21072107

21082108
readonly styles = StyleSheet.create({
21092109
container: {

0 commit comments

Comments
 (0)