Fix ref type compatibility for builtin components (#56673)#56673
Open
huntie wants to merge 1 commit intofacebook:mainfrom
Open
Fix ref type compatibility for builtin components (#56673)#56673huntie wants to merge 1 commit intofacebook:mainfrom
huntie wants to merge 1 commit intofacebook:mainfrom
Conversation
|
@huntie has exported this pull request. If you are a Meta employee, you can view the originating Diff in D103601923. |
huntie
added a commit
to huntie/react-native
that referenced
this pull request
May 5, 2026
Summary: Fix `useRef` and `forwardRef` backcompat when migrating to React Native's generated TypeScript types (Strict TypeScript API). Previously this was an awkward breaking change: https://reactnative.dev/docs/strict-typescript-api#some-core-components-are-now-function-components-instead-of-class-components ```diff - const ref = useRef<View>(null); + const ref = useRef<React.ComponentRef<typeof View>>(null); ``` After this diff, no workaround will be required. **Problem detail** In our Flow source code, built-in components like `View` and `TextInput` are function components rather than classes. This differs from the previous manual `.d.ts` files which defined each component as a class. In TypeScript, a class name serves as both a value (the constructor) and a type (the instance), so patterns like `useRef<View>()` work. However, with function components, the name is only a value (the function signature) making `useRef<View>()` resolve to a ref holding the function itself, not the native element. **The fix**: Add companion type aliases alongside each component's value export. TypeScript's declaration merging makes the component name simultaneously a value (for JSX) and a type (for refs). ```diff // Before — required workaround - const ref = useRef<View>(null); + const ref = useRef<React.ComponentRef<typeof View>>(null); ref.current?.measure(...); - const inputRef = useRef<TextInput>(null); + const inputRef = useRef<React.ComponentRef<typeof TextInput>>(null); inputRef.current?.focus(); // After — original patterns work as-is const ref = useRef<View>(null); // View as a type = HostInstance ref.current?.measure(...); // HostInstance has measure() const inputRef = useRef<TextInput>(null); // TextInput as a type = TextInputInstance inputRef.current?.focus(); // TextInputInstance has focus() ``` Changelog: [General][Fixed] - Add companion instance type aliases for built-in components, preserving `useRef<Component>` patterns under the Strict TypeScript API Differential Revision: D103601923
Summary: Fix `useRef` and `forwardRef` backcompat when migrating to React Native's generated TypeScript types (Strict TypeScript API). Previously this was an awkward breaking change: https://reactnative.dev/docs/strict-typescript-api#some-core-components-are-now-function-components-instead-of-class-components ```diff - const ref = useRef<View>(null); + const ref = useRef<React.ComponentRef<typeof View>>(null); ``` After this diff, no workaround will be required. **Changes** - Add companion type aliases alongside each component's value export. - Introduces a new `/* ts-only */` comment syntax in `index.js.flow` to hide the companion exports from Flow, which has no equivalent concept of declaration merging. **Problem detail** In our Flow source code, built-in components like `View` and `TextInput` are function components rather than classes. This differs from the previous manual `.d.ts` files which [defined each component as a class](https://github.com/facebook/react-native/blob/d1cdce24cca2224f8d37873d24a49523a8271a0a/packages/react-native/Libraries/Text/Text.d.ts#L226). In TypeScript, a class name serves as both a value (the constructor) and a type (the instance), so patterns like `useRef<View>()` work. However, with function components, the name is only a value (the function signature) making `useRef<View>()` resolve to a ref holding the function itself, not the native element. **The fix**: Add companion type aliases alongside each component's value export. TypeScript's declaration merging makes the component name simultaneously a value (for JSX) and a type (for refs). ```diff // Before — required workaround - const ref = useRef<View>(null); // TS2749: 'View' refers to a value, not a type + const ref = useRef<React.ComponentRef<typeof View>>(null); ref.current?.measure(...); - const inputRef = useRef<TextInput>(null); // TS2749: 'TextInput' refers to a value, not a type + const inputRef = useRef<React.ComponentRef<typeof TextInput>>(null); inputRef.current?.focus(); // After — original patterns work as-is const ref = useRef<View>(null); // View as a type = HostInstance ref.current?.measure(...); // HostInstance has measure() const inputRef = useRef<TextInput>(null); // TextInput as a type = TextInputInstance inputRef.current?.focus(); // TextInputInstance has focus() ``` Changelog: [General][Fixed] - Add companion instance type aliases for built-in components, preserving `useRef<Component>` patterns under the Strict TypeScript API Differential Revision: D103601923
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
Fix
useRefandforwardRefbackcompat when migrating to React Native's generated TypeScript types (Strict TypeScript API).Previously this was an awkward breaking change: https://reactnative.dev/docs/strict-typescript-api#some-core-components-are-now-function-components-instead-of-class-components
After this diff, no workaround will be required.
Changes
/* ts-only */comment syntax inindex.js.flowto hide the companion exports from Flow, which has no equivalent concept of declaration merging.Problem detail
In our Flow source code, built-in components like
ViewandTextInputare function components rather than classes. This differs from the previous manual.d.tsfiles which defined each component as a class.In TypeScript, a class name serves as both a value (the constructor) and a type (the instance), so patterns like
useRef<View>()work. However, with function components, the name is only a value (the function signature) makinguseRef<View>()resolve to a ref holding the function itself, not the native element.The fix: Add companion type aliases alongside each component's value export. TypeScript's declaration merging makes the component name simultaneously a value (for JSX) and a type (for refs).
Changelog:
[General][Fixed] - Add companion instance type aliases for built-in components, preserving
useRef<Component>patterns under the Strict TypeScript APIDifferential Revision: D103601923