From 7771fd30e28f892fd9de40baa847d41c24954ecd Mon Sep 17 00:00:00 2001 From: D N <4661784+retyui@users.noreply.github.com> Date: Wed, 2 Dec 2026 16:48:08 +0100 Subject: [PATCH 1/3] fix(types): export the public `ImageCropData` / `CropResult` types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `src/index.ts` and `src/index.web.ts` had `export default ImageEditor` as their only export. `types.ts` ships in the package but nothing re-exports it, so the two types the README names are unreachable: import type { ImageCropData } from '@react-native-community/image-editor'; // TS2614: Module has no exported member 'ImageCropData' Consumers who want to type a crop-data variable have to hand-copy the shape — which is what the example app had to do (`example/src/types.ts`). Both entry points now re-export the same names, so the import works whichever platform bundles. Runtime is untouched; this is `export type` only. --- src/index.ts | 2 ++ src/index.web.ts | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/index.ts b/src/index.ts index abe1225..9485496 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,6 +27,8 @@ const RNCImageEditor: Spec = NativeRNCImageEditor type CropResultWithoutBase64 = Omit; type ImageCropDataWithoutBase64 = Omit; +export type { ImageCropData, CropResult } from './types.ts'; + function toHeadersObject( headers: ImageCropData['headers'] ): Record | undefined { diff --git a/src/index.web.ts b/src/index.web.ts index 4aa8086..817bc44 100644 --- a/src/index.web.ts +++ b/src/index.web.ts @@ -151,4 +151,6 @@ class ImageEditor { } } +export type { ImageCropData, CropResult } from './types.ts'; + export default ImageEditor; From 71d78926906fca1eb29d7cc7f7e9b4ab424c419c Mon Sep 17 00:00:00 2001 From: D N <4661784+retyui@users.noreply.github.com> Date: Wed, 2 Dec 2026 16:53:27 +0100 Subject: [PATCH 2/3] fix(types): key the cropImage result on includeBase64 with one signature MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The three overloads had two holes. A pre-typed `ImageCropData` variable with `includeBase64: true` set at runtime matched overload 1, so `r.base64` was TS2339 although the value was there — the example app's exact pattern, which pushes users to `(r as any).base64`. `includeBase64: someBoolean` matched no overload at all (TS2769) although it is runtime-valid. One generic signature keyed on the literal `includeBase64` value fixes both: `true` -> `base64: string`; `false`/absent -> no `base64`; a `boolean` that isn't statically known -> `base64?: string`, which is the honest answer when the branch can't be picked at compile time. Both entry points now declare the shared `CropImageFunction` shape, and `__typetests__` asserts native and web both satisfy it. That assertion covers the declared signature only, not runtime behavior — reintroducing the old "web always computes base64" bug leaves it clean, because `index.web.ts` casts its return through `as CropResultFor` unconditionally. The comment there says so; a runtime test is the real fix for behavioral parity. --- src/__typetests__/index.ts | 53 ++++++++++++++++++++++++++++++++++++++ src/index.ts | 31 ++++++++-------------- src/index.web.ts | 23 ++++++++++------- src/types.ts | 29 +++++++++++++++++++++ 4 files changed, 107 insertions(+), 29 deletions(-) diff --git a/src/__typetests__/index.ts b/src/__typetests__/index.ts index 98fbc86..8cc6d78 100644 --- a/src/__typetests__/index.ts +++ b/src/__typetests__/index.ts @@ -1,5 +1,13 @@ /* eslint-disable */ import ImageEditorNative from '../index.ts'; +import ImageEditorWeb from '../index.web.ts'; +import type { + CropImageFunction, + CropResult, + ImageCropData, +} from '@react-native-community/image-editor'; +// ^^^ resolved through `tsconfig.json` `paths` to `src/index` — the same +// public entry point consumers use. const requiredParams = { size: { width: 100, height: 100 }, @@ -33,3 +41,48 @@ ImageEditorNative.cropImage('', { // @ts-expect-error - base64 doesn't exist const a: string = e.base64; }); +// --- `includeBase64` whose value isn't statically known (H6) --- +// Must resolve to a result whose `base64` is optional — not to the +// no-`base64` type (which reported `base64` missing although it was present +// at runtime) and not to the required-`base64` type. +const flag: boolean = Math.random() > 0.5; +ImageEditorNative.cropImage('', { + ...requiredParams, + includeBase64: flag, +}).then((e) => { + const a: string | undefined = e.base64; + // @ts-expect-error - `base64` may be undefined + const b: string = e.base64; +}); + +// A pre-typed `ImageCropData` variable (the example app's pattern) infers the +// optional-`base64` result too. Previously overload 1 matched it and reported +// `base64` missing even with `includeBase64: true` set at runtime. +const preTyped: ImageCropData = { ...requiredParams, includeBase64: true }; +ImageEditorNative.cropImage('', preTyped).then((e) => { + const a: string | undefined = e.base64; +}); + +// --- the public types are importable from the package (H5) --- +// Fails with TS2614 if the entry points stop re-exporting them, and fails if +// the shape drifts: `ImageCropData` requires `offset`/`size`, so `{}` wouldn't +// satisfy it. +const typedParams: ImageCropData = requiredParams; +ImageEditorNative.cropImage('', typedParams); +const typedResult: Promise = ImageEditorNative.cropImage('', { + ...requiredParams, + includeBase64: true, +}); +void typedResult; + +// --- native and web have the same `cropImage` signature (H7) --- +// This checks the *declared* signature only: it catches one entry point's +// type drift, not its runtime behavior. Verified negative — reintroducing +// the old "web always computes `base64`" bug leaves this file clean, since +// `index.web.ts` casts its return through `as CropResultFor` unconditionally. +// Nothing in this repo checks that each platform *implements* `includeBase64`; +// that still needs a runtime test. +const nativeImpl: CropImageFunction = ImageEditorNative.cropImage; +const webImpl: CropImageFunction = ImageEditorWeb.cropImage; +void nativeImpl; +void webImpl; diff --git a/src/index.ts b/src/index.ts index 9485496..c7a45cf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,7 +8,7 @@ import { Platform } from 'react-native'; import NativeRNCImageEditor from './NativeRNCImageEditor'; import type { Spec } from './NativeRNCImageEditor'; -import type { ImageCropData, CropResult } from './types.ts'; +import type { ImageCropData, CropResultFor } from './types.ts'; const LINKING_ERROR = `The package '@react-native-community/image-editor' doesn't seem to be linked. Make sure: \n\n` + @@ -24,10 +24,7 @@ const RNCImageEditor: Spec = NativeRNCImageEditor }, }); -type CropResultWithoutBase64 = Omit; -type ImageCropDataWithoutBase64 = Omit; - -export type { ImageCropData, CropResult } from './types.ts'; +export type { ImageCropData, CropResult, CropImageFunction } from './types.ts'; function toHeadersObject( headers: ImageCropData['headers'] @@ -51,25 +48,19 @@ class ImageEditor { * cropped image from the cache path when you are done with it. */ - // TS overload for better `base64` type inference (see: `src/__typetests__/index.ts`) - static cropImage( - uri: string, - cropData: ImageCropDataWithoutBase64 - ): Promise; - static cropImage( - uri: string, - cropData: ImageCropDataWithoutBase64 & { includeBase64: false } - ): Promise; - static cropImage( + /** + * The result type follows `includeBase64`: `base64` is required when it is + * `true`, absent when it is `false` or omitted, and optional when the value + * isn't statically known (see `CropImageFunction`). + */ + static cropImage( uri: string, - cropData: ImageCropDataWithoutBase64 & { includeBase64: true } - ): Promise; - - static cropImage(uri: string, cropData: ImageCropData): Promise { + cropData: Omit & { includeBase64?: B } + ): Promise> { return RNCImageEditor.cropImage(uri, { ...cropData, headers: toHeadersObject(cropData.headers), - }) as Promise; + }) as Promise>; } } diff --git a/src/index.web.ts b/src/index.web.ts index 817bc44..47be22d 100644 --- a/src/index.web.ts +++ b/src/index.web.ts @@ -1,4 +1,4 @@ -import type { ImageCropData, CropResult } from './types.ts'; +import type { CropResult, CropResultFor, ImageCropData } from './types.ts'; const ERROR_PREFIX = 'ImageEditor: '; @@ -76,13 +76,13 @@ function fetchImage( const DEFAULT_COMPRESSION_QUALITY = 0.9; class ImageEditor { - static cropImage( + // Signature shared with the native entry point via `CropImageFunction` + // (asserted in `src/__typetests__/index.ts`); `includeBase64` decides + // whether `base64` is part of the result. + static cropImage( imgSrc: string, - cropData: ImageCropData - ): Promise { - /** - * Returns a promise that resolves with the base64 encoded string of the cropped image - */ + cropData: Omit & { includeBase64?: B } + ): Promise> { return fetchImage(imgSrc, cropData.headers).then( function onfulfilledImgToCanvas(image) { const ext = cropData.format ?? 'jpeg'; @@ -144,13 +144,18 @@ class ImageEditor { }); } - return result; + return result as CropResultFor; }); } ); } } -export type { ImageCropData, CropResult } from './types.ts'; +export type { + CropResultFor, + ImageCropData, + CropResult, + CropImageFunction, +} from './types.ts'; export default ImageEditor; diff --git a/src/types.ts b/src/types.ts index e39d97b..14eb320 100644 --- a/src/types.ts +++ b/src/types.ts @@ -21,6 +21,35 @@ export interface CropResult extends Omit< // ^^^ codegen doesn't support union types yet } +/** + * `cropImage` result for a given `includeBase64` value. + * + * `boolean` (a value not known at compile time) resolves to the full + * `CropResult`, whose `base64` is optional — the honest answer when the + * branch can't be picked statically. + */ +export type CropResultFor = [B] extends [true] + ? CropResult & { base64: string } + : [B] extends [false] + ? Omit + : CropResult; + +/** + * The `cropImage` signature, shared by the native and web entry points. + * + * The result type keys off the literal `includeBase64` value: `true` → + * `base64: string` present; `false`/absent → `base64` absent; a `boolean` + * that isn't statically known → `base64?: string`, which is the honest + * answer for a pre-typed `ImageCropData` variable. + * + * `src/__typetests__/index.ts` asserts both entry points implement it, so + * the platforms can't drift apart silently. + */ +export type CropImageFunction = ( + uri: string, + cropData: Omit & { includeBase64?: B } +) => Promise>; + // Utils type AsyncReturnType = T extends (...args: any[]) => Promise ? R From a3b72caebe72d44934842e8259353e20aa5f2060 Mon Sep 17 00:00:00 2001 From: D N <4661784+retyui@users.noreply.github.com> Date: Wed, 2 Dec 2026 16:53:53 +0100 Subject: [PATCH 3/3] chore: add changeset for exported types and conditional result type --- .changeset/api-types-and-conditional-result.md | 11 +++++++++++ src/types.ts | 5 +++-- 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 .changeset/api-types-and-conditional-result.md diff --git a/.changeset/api-types-and-conditional-result.md b/.changeset/api-types-and-conditional-result.md new file mode 100644 index 0000000..0207e3b --- /dev/null +++ b/.changeset/api-types-and-conditional-result.md @@ -0,0 +1,11 @@ +--- +'@react-native-community/image-editor': minor +--- + +Export the public `ImageCropData` and `CropResult` types, and key the `cropImage` result on `includeBase64` + +`import type { ImageCropData } from '@react-native-community/image-editor'` failed with TS2614 — the README names these types but the entry points only had a default export. Both native and web entry points now re-export them, along with `CropImageFunction` and `CropResultFor`. + +`cropImage`'s three overloads are replaced by one generic signature keyed on the literal `includeBase64` value, fixing two holes: a pre-typed `ImageCropData` variable with `includeBase64: true` reported `r.base64` as missing (TS2339) although it was present at runtime, and `includeBase64: someBoolean` matched no overload (TS2769) although it is runtime-valid. `includeBase64` whose value isn't statically known now resolves to `base64?: string`. + +If you worked around the first hole with `(r as any).base64`, that cast is no longer needed. diff --git a/src/types.ts b/src/types.ts index 14eb320..4f722fe 100644 --- a/src/types.ts +++ b/src/types.ts @@ -42,8 +42,9 @@ export type CropResultFor = [B] extends [true] * that isn't statically known → `base64?: string`, which is the honest * answer for a pre-typed `ImageCropData` variable. * - * `src/__typetests__/index.ts` asserts both entry points implement it, so - * the platforms can't drift apart silently. + * `src/__typetests__/index.ts` asserts both entry points declare this + * signature. It catches type drift only: each implementation's *behavior* + * (notably honoring `includeBase64`) needs a runtime test to cover it. */ export type CropImageFunction = ( uri: string,