Skip to content

fix(types): stop the CodegenTypes deep import from erasing numeric types - #219

Closed
retyui wants to merge 1 commit into
masterfrom
fix/types-real-codegen-scalars
Closed

retyui wants to merge 1 commit into
masterfrom
fix/types-real-codegen-scalars

Conversation

@retyui

@retyui retyui commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Problem

src/NativeRNCImageEditor.ts is both the Codegen spec and the source of the published .d.ts. It imported the Codegen scalar aliases through a deep import:

import type { Double, Float, Int32 } from 'react-native/Libraries/Types/CodegenTypes';

That specifier has no resolvable types under React Native's own exports map. ./Libraries/* resolves to { react-native-legacy-deep-imports: "./Libraries/*.d.ts", types: null, default: "./Libraries/*.js" }, and there is no exact entry for ./Libraries/Types/CodegenTypes — the real declarations live at types_generated/Libraries/Types/CodegenTypes.d.ts. The import only survived yarn ts because it carried an inline @ts-expect-error.

TypeScript therefore resolved Double, Float and Int32 to any — and since this file is the published declaration, the any reached every consumer. The entire numeric surface of the public API stopped type-checking.

Evidence

Before the change, these all compiled cleanly (verified: tsc reports Unused '@ts-expect-error' directive for each):

ImageEditor.cropImage('u', { offset: { x: 'oops', y: 0 }, size: { width: 100, height: 100 } });
ImageEditor.cropImage('u', { offset: { x: 0, y: 0 }, size: { width: 'wide', height: 100 } });
ImageEditor.cropImage('u', { ...p, quality: 'high' });
const w: string = (await ImageEditor.cropImage('u', p)).width;

After the change all four are errors, and yarn ts passes.

The change

Declare the three aliases locally. Codegen matches these scalars by identifier name and performs no module resolution, so the schema semantics are preserved.

Generated native code is unchanged — verified, not assumed

I ran the actual Codegen generators against the schema from both versions of the spec and diffed the output:

Generator Result
GenerateModuleJavaSpec (Android legacy) byte-identical
GenerateModuleObjCpp (iOS) byte-identical
GenerateModuleJniH + GenerateModuleJniCpp (bridgeless Android) byte-identical

The intermediate schema does differ in one respect: 10 fields move from Double/Float/Int32TypeAnnotation to NumberTypeAnnotation. That is inert — every generator folds all four onto the same target, and I checked each mapping rather than trusting it:

  • GenerateModuleJavaSpec.js:154-163 → all four → double
  • GenerateModuleJniCpp.js:131-140 → all four → NumberKind; :210-217 and :285-291 → all four → D / Ljava/lang/Double;

Nothing in the repo checks in a generated schema, and nothing consumes the annotation names (git ls-files | grep schema → none). No new or lost fields; structure is identical modulo those annotation names.

Reproduce

node node_modules/@react-native/codegen/lib/cli/combine/combine-js-to-schema-cli.js -l RNCImageEditorSpec /tmp/s.json src/NativeRNCImageEditor.ts
yarn ts && yarn build

Compatibility

No runtime change and no change to generated native code, so no new breakage. Note the flip side: consumers who were relying on these fields being any may now see new type errors in their own code where they passed a string where a number was wanted. That is the bug being fixed, but it is a visible effect of a patch/minor bump and worth calling out in the release notes.

Notes

  • android/ios are not compiled here — no local toolchain for them (the gradle wrapper fails its TLS handshake in this environment). This PR touches only TypeScript, so yarn ts + yarn build + the generator diffs above are the full verification.
  • Found during a broader review; the rest of the findings are being filed as separate PRs.

`react-native/Libraries/Types/CodegenTypes` has no resolvable types under
React Native's `exports` map (`./Libraries/*` sets `"types": null`), so the
import needed a `@ts-expect-error` to survive `yarn ts`. TypeScript resolved
`Double`/`Float`/`Int32` to `any`, and because the spec is the source of the
published `.d.ts`, that `any` propagated to every consumer: `offset.x`,
`size.width`, `displaySize`, `quality`, and the result `width`/`height`/`size`
all silently stopped type-checking.

Declare the three aliases locally instead. Codegen matches these scalars by
identifier name and does no module resolution, so the generated native code is
unchanged: the Java spec, the ObjC++ spec and the bridgeless JNI spec are all
byte-identical before and after (the intermediate schema renames
`Double/Float/Int32TypeAnnotation` to `NumberTypeAnnotation`, and every
generator maps all four to `double` / `NumberKind` / `D`).

Adds typetest directives that fail with "Unused '@ts-expect-error'" if the
erasure ever comes back.
@retyui retyui closed this Dec 2, 2026
@retyui
retyui deleted the fix/types-real-codegen-scalars branch December 2, 2026 13:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant