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

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 https://github.com/react/react-native/issues/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>`

Pull Request resolved: https://github.com/react/react-native/pull/58102

Test Plan: Tested locally on a library generated with Bob.

Reviewed By: cipolleschi

Differential Revision: D117336232

Pulled By: fabriziocucci

fbshipit-source-id: 173fcfbc568cc4a8e79c32d16fbcee988db9b104
This commit is contained in:
artus9033 authored and meta-codesync[bot] committed 2026-08-26 12:00:32 -07:00
1 parent f826318c5f
commit 391723e869
2 files changed
+8 -5

No files matched your search

@@ -23,6 +23,10 @@ type NativeComponentOptions = Readonly<{
excludedPlatforms?: ReadonlyArray<'iOS' | 'android'>,
}>;
// Note that this alias is not re-exported from the `react-native` root,
// so it must not be used in public interfaces, such as the declared return type of
// `codegenNativeComponent` below: consumers that emit declaration files would be
// unable to name it portably (TS2883).
export type NativeComponentType<T extends {...}> = HostComponent<T>;
// If this function runs then that means the view configs were not
@@ -34,7 +38,7 @@ export type NativeComponentType<T extends {...}> = HostComponent<T>;
function codegenNativeComponent<Props extends {...}>(
componentName: string,
options?: NativeComponentOptions,
): NativeComponentType<Props> {
): HostComponent<Props> {
if (global.RN$Bridgeless === true && __DEV__) {
console.warn(
`Codegen didn't run for ${componentName}. This will be an error in the future. Make sure you are using @react-native/babel-preset when building your JavaScript code.`,
+3 -4
View File
@@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<650ec5a18cd69ff8783eaeaf894868fb>>
* @generated SignedSource<<ac7ab1ffce99d02fd6c918a6ed1fb85c>>
*
* This file was generated by scripts/js-api/build-types/index.js.
*/
@@ -1737,7 +1737,7 @@ declare type codegenNativeComponent = typeof codegenNativeComponent
declare function codegenNativeComponent_default<Props extends {}>(
componentName: string,
options?: NativeComponentOptions,
): NativeComponentType<Props>
): HostComponent<Props>
declare namespace CodegenTypes {
export {
BubblingEventHandler,
@@ -3085,7 +3085,6 @@ declare type NativeComponentOptions = {
declare namespace NativeComponentRegistry {
export { setRuntimeConfigProvider, get, getWithFallback_DEPRECATED }
}
declare type NativeComponentType<T extends {}> = HostComponent<T>
declare class NativeEventEmitter<
TEventToArgsMap extends Readonly<
Record<string, ReadonlyArray<UnsafeNativeEventObject>>
@@ -6047,7 +6046,7 @@ export {
VirtualizedSectionListProps, // f42f54c4
WrapperComponentProvider, // 9ef54e61
codegenNativeCommands, // 628a7c0a
codegenNativeComponent, // 32a1bca6
codegenNativeComponent, // 520daa94
findNodeHandle, // 93f80214
processColor, // 6e877698
registerCallableModule, // 839c8cfe