From f9fa087d7bca5e6d90a37bf222b902f48dfbf1e3 Mon Sep 17 00:00:00 2001 From: D N <4661784+retyui@users.noreply.github.com> Date: Wed, 2 Dec 2026 14:19:46 +0100 Subject: [PATCH] fix(types): stop the CodegenTypes deep import from erasing numeric types `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. --- src/NativeRNCImageEditor.ts | 22 ++++++++++++++++------ src/__typetests__/index.ts | 24 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/NativeRNCImageEditor.ts b/src/NativeRNCImageEditor.ts index 33e6e17..d5fcc26 100644 --- a/src/NativeRNCImageEditor.ts +++ b/src/NativeRNCImageEditor.ts @@ -1,12 +1,22 @@ import type { TurboModule } from 'react-native'; -import type { - Double, - Float, - Int32, - // @ts-expect-error TS2307 Cannot find module '../CodegenTypes' -} from 'react-native/Libraries/Types/CodegenTypes'; import { TurboModuleRegistry } from 'react-native'; +/** + * Codegen scalar aliases, declared locally instead of imported from + * `react-native/Libraries/Types/CodegenTypes`. + * + * That deep import has no resolvable types under React Native's own + * `exports` map (`./Libraries/*` sets `"types": null`), so importing it + * silently degrades every `Double`/`Float`/`Int32` below to `any` for this + * library *and* for every consumer of the published `.d.ts` — the whole + * numeric surface of the public API stops type-checking. Codegen matches + * these scalars by identifier name and does no module resolution, so local + * aliases produce a byte-identical schema. + */ +type Double = number; +type Float = number; +type Int32 = number; + export interface Spec extends TurboModule { cropImage( uri: string, diff --git a/src/__typetests__/index.ts b/src/__typetests__/index.ts index 98fbc86..374e77f 100644 --- a/src/__typetests__/index.ts +++ b/src/__typetests__/index.ts @@ -33,3 +33,27 @@ ImageEditorNative.cropImage('', { // @ts-expect-error - base64 doesn't exist const a: string = e.base64; }); + +// Regression guard for the `CodegenTypes` deep import collapsing every +// `Double`/`Float`/`Int32` in the public API to `any`. If that import ever +// comes back, the directives below become "Unused '@ts-expect-error'" and +// `yarn ts` fails. See `src/NativeRNCImageEditor.ts`. +ImageEditorNative.cropImage('', { + // @ts-expect-error - offset.x must be a number + offset: { x: 'oops', y: 0 }, + size: { width: 100, height: 100 }, +}); +ImageEditorNative.cropImage('', { + offset: { x: 0, y: 0 }, + // @ts-expect-error - size.width must be a number + size: { width: 'wide', height: 100 }, +}); +ImageEditorNative.cropImage('', { + ...requiredParams, + // @ts-expect-error - quality must be a number + quality: 'high', +}); +ImageEditorNative.cropImage('', requiredParams).then((e) => { + // @ts-expect-error - width is a number + const w: string = e.width; +});