Skip to content

fix(types): export public types; key cropImage result on includeBase64 - #223

Closed
retyui wants to merge 3 commits into
fix/web-uri-and-includebase64from
fix/api-exports-and-conditional-result
Closed

retyui wants to merge 3 commits into
fix/web-uri-and-includebase64from
fix/api-exports-and-conditional-result

Conversation

@retyui

@retyui retyui commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Stacked on #220 — targets fix/web-uri-and-includebase64, not master. Rebase onto master after #220 merges (the web signature in the second commit depends on #220's includeBase64 fix; without it index.web.ts still ignores the flag). Two commits, one finding each.

7771fd3 — the public types aren't exported

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 by name 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 ended up doing (example/src/types.ts). Both entry points now re-export the same names, so the import works whichever platform bundles. export type only; runtime untouched.

71d7892 — the overload set has two holes

static cropImage(uri, cropData: ImageCropDataWithoutBase64): Promise<CropResultWithoutBase64>;
static cropImage(uri, cropData: ... & { includeBase64: false }): Promise<CropResultWithoutBase64>;
static cropImage(uri, cropData: ... & { includeBase64: true }): Promise<CropResultWithoutBase64 & { base64: string }>;
  • Silent wrong type. A pre-typed ImageCropData variable matches overload 1 even when includeBase64: true is set at runtime, so r.base64 is TS2339 while the value is present. That's the example app's exact pattern, and it pushes people to (r as any).base64.
  • Valid call rejected. includeBase64: someBoolean hits no overload → TS2769, though it's runtime-valid.

One generic signature fixes both:

static cropImage<B extends boolean = false>(
  uri: string,
  cropData: Omit<ImageCropData, 'includeBase64'> & { includeBase64?: B }
): Promise<CropResultFor<B>>;

true → base64: string; false/absent → no base64; a boolean not known at compile time → base64?: string, which is the honest answer when the branch can't be picked statically. CropResultFor uses [B] extends [true] (not B extends true) so a naked boolean doesn't distribute into a union of both arms.

The parity assertion is weaker than I first believed — and this PR says so in the code

__typetests__ asserts both entry points satisfy the shared CropImageFunction. I intended that to also catch web's runtime drift, then negative-tested it and it does not: reintroducing the pre-#220 bug (web always computing base64) leaves the file clean, because index.web.ts casts its return through as CropResultFor<B> unconditionally. So the assertion checks declared signatures only, not that each platform implements includeBase64. The comment at src/__typetests__/index.ts now states this limit explicitly rather than claiming a guarantee it doesn't give. Real behavioral parity needs a runtime test — out of scope here (the test-infrastructure PR is the right home for it).

I also negative-tested the parts that do bite: degenerating CropResultFor to a plain alias produces TS2322 at __typetests__/index.ts:22, so the conditional-type assertions aren't vacuous.

Verification

yarn tsc and eslint src/ clean. The H5 assertions are load-bearing by construction: the typetests import through @react-native-community/image-editor (resolved to src/index by tsconfig.json paths), i.e. the same entry point consumers use, so TS2614 returns if the exports are dropped. ImageCropData requires offset/size, so const typedParams: ImageCropData = requiredParams also fails if the shape drifts.

Behavior change: new type errors are the point — code that passed a non-boolean to includeBase64, or relied on r.base64 being absent for a pre-typed variable, may now fail to compile. Hence the changeset's minor bump.

Part of a batch: #219 (types/codegen), #220 (web), #221 (Android), #222 (iOS).

`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.
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<B>` unconditionally. The comment there
says so; a runtime test is the real fix for behavioral parity.
@retyui
retyui force-pushed the fix/api-exports-and-conditional-result branch from 221fbaf to a3b72ca Compare December 2, 2026 16:15
@retyui retyui closed this Dec 2, 2026
@retyui
retyui deleted the fix/api-exports-and-conditional-result branch December 2, 2026 19:07
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