Skip to content

fix: make codegenNativeComponent's return type nameable by consumers - #58102

Open
artus9033 wants to merge 1 commit into
react:mainfrom
artus9033:fix/codegen-native-component-portable-return-type
Open

fix: make codegenNativeComponent's return type nameable by consumers#58102
artus9033 wants to merge 1 commit into
react:mainfrom
artus9033:fix/codegen-native-component-portable-return-type

Conversation

@artus9033

Copy link
Copy Markdown
Contributor

Summary:

codegenNativeComponent() declared its return type as NativeComponentType<Props>. That alias lives in Libraries/Utilities/codegenNativeComponent.js and is not re-exported from the react-native root, and the package's exports map has no ./types_generated/* subpath, so the deep specifier is not resolvable either.

As a result, any library that calls codegenNativeComponent() and emits declaration files fails to build under moduleResolution: node16/nodenext/bundler - TypeScript cannot name the inferred return type portably:

src/ReactNativeTestViewNativeComponent.ts:11:1 - error TS2883: The inferred type of 'default' cannot be named without a reference to 'NativeComponentType' from '../node_modules/react-native/types_generated/Libraries/Utilities/codegenNativeComponent'. This is likely not portable. A type annotation is necessary.
  
  11 export default codegenNativeComponent<NativeProps>('ReactNativeTestView');
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This is the default fabric-view template from create-react-native-library, so every new Fabric view library hits it.
It used to work before RN 0.87.0, until #57490 has removed visibility of the internally exported types.

Example of breakage - this test run in react-native-builder-bob: https://github.com/callstack/react-native-builder-bob/actions/runs/32675382955/job/97282442650#step:19:20

Fix

The fix is to declare the return type as HostComponent<Props> instead. NativeComponentType<T> is defined as HostComponent<T>, but only HostComponent is exported from root. Consumers can reach it via import('react-native').HostComponent<Props>.

Changelog:

[GENERAL] [FIXED] - Fix TS2883 when building declaration files for libraries that use codegenNativeComponent due to unreachable NativeComponentType<T>

Test Plan:

Tested locally on a library generated with Bob.

@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 Aug 24, 2026
@github-actions

Copy link
Copy Markdown

Warning

JavaScript API change detected

This PR commits an update to ReactNativeApi.d.ts, indicating a change to React Native's public JavaScript API.

  • Please include a clear changelog message.
  • This change will be subject to additional review.

This change was flagged as: POTENTIALLY_BREAKING

@artus9033

Copy link
Copy Markdown
Contributor Author

CC @huntie

@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Aug 24, 2026

@huntie huntie left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Endorsed. Thanks 🙏🏻

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. p: Callstack Partner: Callstack Partner Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants