Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .changeset/api-types-and-conditional-result.md
Original file line number Diff line number Diff line change
@@ -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.
53 changes: 53 additions & 0 deletions src/__typetests__/index.ts
Original file line number Diff line number Diff line change
@@ -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 },
Expand Down Expand Up @@ -33,3 +41,48 @@ ImageEditorNative.cropImage('<test>', {
// @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('<test>', {
...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('<test>', 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('<test>', typedParams);
const typedResult: Promise<CropResult> = ImageEditorNative.cropImage('<test>', {
...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<B>` 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;
29 changes: 11 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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` +
Expand All @@ -24,8 +24,7 @@ const RNCImageEditor: Spec = NativeRNCImageEditor
},
});

type CropResultWithoutBase64 = Omit<CropResult, 'base64'>;
type ImageCropDataWithoutBase64 = Omit<ImageCropData, 'includeBase64'>;
export type { ImageCropData, CropResult, CropImageFunction } from './types.ts';

function toHeadersObject(
headers: ImageCropData['headers']
Expand All @@ -49,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<CropResultWithoutBase64>;
static cropImage(
uri: string,
cropData: ImageCropDataWithoutBase64 & { includeBase64: false }
): Promise<CropResultWithoutBase64>;
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<B extends boolean = false>(
uri: string,
cropData: ImageCropDataWithoutBase64 & { includeBase64: true }
): Promise<CropResultWithoutBase64 & { base64: string }>;

static cropImage(uri: string, cropData: ImageCropData): Promise<CropResult> {
cropData: Omit<ImageCropData, 'includeBase64'> & { includeBase64?: B }
): Promise<CropResultFor<B>> {
return RNCImageEditor.cropImage(uri, {
...cropData,
headers: toHeadersObject(cropData.headers),
}) as Promise<CropResult>;
}) as Promise<CropResultFor<B>>;
}
}

Expand Down
23 changes: 15 additions & 8 deletions src/index.web.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ImageCropData, CropResult } from './types.ts';
import type { CropResult, CropResultFor, ImageCropData } from './types.ts';

const ERROR_PREFIX = 'ImageEditor: ';

Expand Down Expand Up @@ -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<B extends boolean = false>(
imgSrc: string,
cropData: ImageCropData
): Promise<CropResult> {
/**
* Returns a promise that resolves with the base64 encoded string of the cropped image
*/
cropData: Omit<ImageCropData, 'includeBase64'> & { includeBase64?: B }
): Promise<CropResultFor<B>> {
return fetchImage(imgSrc, cropData.headers).then(
function onfulfilledImgToCanvas(image) {
const ext = cropData.format ?? 'jpeg';
Expand Down Expand Up @@ -144,11 +144,18 @@ class ImageEditor {
});
}

return result;
return result as CropResultFor<B>;
});
}
);
}
}

export type {
CropResultFor,
ImageCropData,
CropResult,
CropImageFunction,
} from './types.ts';

export default ImageEditor;
30 changes: 30 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,36 @@ 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 boolean> = [B] extends [true]
? CropResult & { base64: string }
: [B] extends [false]
? Omit<CropResult, 'base64'>
: 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 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 = <B extends boolean = false>(
uri: string,
cropData: Omit<ImageCropData, 'includeBase64'> & { includeBase64?: B }
) => Promise<CropResultFor<B>>;

// Utils
type AsyncReturnType<T> = T extends (...args: any[]) => Promise<infer R>
? R
Expand Down
Loading