From 391723e869451b0d9b479dc4f39c5beb48de4315 Mon Sep 17 00:00:00 2001 From: artus9033 Date: Wed, 26 Aug 2026 12:00:32 -0700 Subject: [PATCH] fix: make `codegenNativeComponent`'s return type nameable by consumers (#58102) Summary: `codegenNativeComponent()` declared its return type as `NativeComponentType`. 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('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` instead. `NativeComponentType` is defined as `HostComponent`, but only `HostComponent` is exported from root. Consumers can reach it via `import('react-native').HostComponent`. ## Changelog: [GENERAL] [FIXED] - Fix TS2883 when building declaration files for libraries that use `codegenNativeComponent` due to unreachable `NativeComponentType` 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 --- .../Libraries/Utilities/codegenNativeComponent.js | 6 +++++- packages/react-native/ReactNativeApi.d.ts | 7 +++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/react-native/Libraries/Utilities/codegenNativeComponent.js b/packages/react-native/Libraries/Utilities/codegenNativeComponent.js index cb5ece90df6..38a664f90c9 100644 --- a/packages/react-native/Libraries/Utilities/codegenNativeComponent.js +++ b/packages/react-native/Libraries/Utilities/codegenNativeComponent.js @@ -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 = HostComponent; // If this function runs then that means the view configs were not @@ -34,7 +38,7 @@ export type NativeComponentType = HostComponent; function codegenNativeComponent( componentName: string, options?: NativeComponentOptions, -): NativeComponentType { +): HostComponent { 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.`, diff --git a/packages/react-native/ReactNativeApi.d.ts b/packages/react-native/ReactNativeApi.d.ts index b3f0f5632e6..5316dcc83cf 100644 --- a/packages/react-native/ReactNativeApi.d.ts +++ b/packages/react-native/ReactNativeApi.d.ts @@ -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<> * * 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( componentName: string, options?: NativeComponentOptions, -): NativeComponentType +): HostComponent declare namespace CodegenTypes { export { BubblingEventHandler, @@ -3085,7 +3085,6 @@ declare type NativeComponentOptions = { declare namespace NativeComponentRegistry { export { setRuntimeConfigProvider, get, getWithFallback_DEPRECATED } } -declare type NativeComponentType = HostComponent declare class NativeEventEmitter< TEventToArgsMap extends Readonly< Record> @@ -6047,7 +6046,7 @@ export { VirtualizedSectionListProps, // f42f54c4 WrapperComponentProvider, // 9ef54e61 codegenNativeCommands, // 628a7c0a - codegenNativeComponent, // 32a1bca6 + codegenNativeComponent, // 520daa94 findNodeHandle, // 93f80214 processColor, // 6e877698 registerCallableModule, // 839c8cfe