Skip to content

Add type-test for View imperative handle methods#57465

Draft
sbalagan22 wants to merge 1 commit into
react:mainfrom
sbalagan22:fix/54104-view-ref-typetest
Draft

Add type-test for View imperative handle methods#57465
sbalagan22 wants to merge 1 commit into
react:mainfrom
sbalagan22:fix/54104-view-ref-typetest

Conversation

@sbalagan22

@sbalagan22 sbalagan22 commented Jul 7, 2026

Copy link
Copy Markdown

Summary:

Calling imperative handle methods on a View ref works through React.ComponentRef<typeof View>, but the public type-tests only covered measure, measureInWindow, and setNativeProps, leaving measureLayout with no type-level regression guard. This adds a focused type-test that pins the full handle contract against both the legacy and the generated strict public types.

Root cause of the gap: a ref to a host component resolves to its imperative handle (ReactNativeElement under the strict types), not to the View component type. The component type is a function and does not carry the native methods, so the type that must be exercised is React.ComponentRef<typeof View>. Without a test asserting measureLayout on that type, a change to the handle surface could drop the method from the public types without failing CI.

Fix: add packages/react-native/__typetests__/view-ref.tsx asserting measure, measureInWindow, measureLayout, and setNativeProps on React.ComponentRef<typeof View>. The file lives in the shared __typetests__ directory, so it is type-checked against both types/ (legacy) and types_generated/ (strict, via react-native-strict-api).

On ViewInstance (per @huntie's note on #54104)

Checked how the new ViewInstance / per-component *Instance types affect this test. Findings from the repo:

Because this test is checked against both the legacy and strict variants, it stays on React.ComponentRef<typeof View>: that is the spelling valid under both, whereas ViewInstance would not resolve under the legacy variant. Once the strict API is the default, a strict-only follow-up could assert the same contract through useRef<ViewInstance> directly.

Changelog:

[INTERNAL] [ADDED] - Add type-test for View imperative handle methods

Test Plan:

Both type-check suites pass with the new file, and the file lints clean.

$ yarn test-typescript
$ tsc -p packages/react-native/types/tsconfig.json
(exit 0)

$ yarn test-generated-typescript
$ tsc -p packages/react-native/types_generated/tsconfig.test.json
(exit 0)

$ eslint --max-warnings 0 packages/react-native/__typetests__/view-ref.tsx
(exit 0)

Confirmed the test guards the contract: adding a call to a non-existent method on the ref (view.nonExistentMethod123()) fails both suites with TS2339: Property 'nonExistentMethod123' does not exist on type 'ReactNativeElement' (strict) and ... on type 'View' (legacy). Removing that line restores a clean pass.

This does not change the behavior reported in #54104 that useRef<View>().measure() errors under the strict types; per @huntie that is addressed for app code in 0.87 via useRef<ViewInstance>. This PR guards the existing supported pattern and closes the measureLayout coverage gap.

Refs #54104

@meta-cla

meta-cla Bot commented Jul 7, 2026

Copy link
Copy Markdown

Hi @sbalagan22!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla

meta-cla Bot commented Jul 7, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 7, 2026
@@ -0,0 +1,59 @@
/**

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Can we rename this file component-refs.tsx?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, renamed to component-refs.tsx.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Can we rename this file component-refs.tsx?

Thanks again for taking the time to review and merge this, it was a great first contribution to React Native for me. I'd really like to keep contributing here, so if there are areas that could use help or issues you'd point a newer contributor toward, I'd be glad to pick them up. Would love to stay in touch . https://www.linkedin.com/in/sukhmanbalagan/

A ref to a host component resolves to its imperative handle
(ReactNativeElement under the strict public types), which is where the
native measurement and mutation methods live. The View component type
itself is not that handle, so `React.ComponentRef<typeof View>` is the
type that resolves to it under both the legacy and the strict public
types.

The existing type-tests exercise measure, measureInWindow, and
setNativeProps but never measureLayout, so the handle contract had a
coverage gap. Add a focused type-test that pins measure, measureInWindow,
measureLayout, and setNativeProps on `React.ComponentRef<typeof View>`,
checked against both the legacy (types/) and generated strict
(types_generated/) definitions.
@sbalagan22 sbalagan22 force-pushed the fix/54104-view-ref-typetest branch from f02da11 to 886ab81 Compare July 7, 2026 18:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants