Donaldcwl / browser-image-compression

Image compression in web browser
MIT License
1.3k stars 160 forks source link

fix: Change function to return File instead of Blob #203

Open KantaHasegawa opened 1 year ago

KantaHasegawa commented 1 year ago

Why

According to index.d.ts, the imageCompression(compress) function is expected to return a File object. https://github.com/Donaldcwl/browser-image-compression/blob/d933bc8e483a9853ed2b57338e035e8c45e40dc7/lib/index.d.ts#L32 However, it currently returns a Blob object rather than a File.

This Blob is not a pure Blob type, but a custom Blob type that has been extended with the name and lastModified properties.

In TypeScript's built-in type definitions, a Blob type object that has the lastModified and name properties is considered a File type. This is indicated in the lib.dom.d.ts file of TypeScript:

typescript/lib/lib.dom.d.ts

/** Provides information about files and allows JavaScript in a web page to access their content. */
interface File extends Blob {
    readonly lastModified: number;
    readonly name: string;
}

Therefore, since the Blob returned by this function has the lastModified and name properties, it can be safely converted to a File type without issues.

What I Did

I have updated the return types of the following three functions from Blob to File:

imageCompression(compress) getFilefromDataUrl canvasToFile

before

before

after

after
KantaHasegawa commented 1 year ago

cc. @Donaldcwl