Closed Seanmclem closed 4 years ago
+1
declare module 'browser-image-compression' { interface Options { maxSizeMB: number; maxWidthOrHeight: number; useWebWorker: boolean; } function imageCompression(image: Blob, options: Options): Blob; export = imageCompression; }
Thanks. I have added the type file in the project and released in version v1.0.7.
Hi @Donaldcwl,
Unfortunately this causes some issues as properties are now required rather than optional.
Example:
Type 'number | undefined' is not assignable to type 'number'.
Type 'undefined' is not assignable to type 'number'. TS2322
166 | return new Promise((resolve, reject) => {
167 | const compressedFile = imageCompression(file, {
> 168 | maxSizeMB: props.compressMaxMB,
| ^
169 | maxWidthOrHeight: props.compressMaxWidthOrHeight,
170 | useWebWorker: true,
171 | })
Those properties all seem optional, meaning for type definitions, you would have to change them to:
interface Options {
maxSizeMB?: number;
maxWidthOrHeight?: number;
useWebWorker?: boolean;
maxIteration?: number,
exifOrientation?: number,
progress?: Function,
fileType?: string
}
Notice the question mark (?) after the property name, which means they can be undefined
which is possible given you have default set in your code.
Also the return type of imageCompression
should be Promise<File | Blob>
as https://github.com/Donaldcwl/browser-image-compression/blob/master/lib/image-compression.js#L22 states.
declare module 'browser-image-compression' {
interface Options {
maxSizeMB?: number;
maxWidthOrHeight?: number;
useWebWorker?: boolean;
maxIteration?: number,
exifOrientation?: number,
progress?: Function,
fileType?: string
}
function imageCompression (image: Blob, options: Options): Promise<Blob>;
export = imageCompression;
}
I will send a PR for you to make it easier.
Thanks @pzi, I am preparing it in the dev branch
I am also planning to submit the type file to https://github.com/DefinitelyTyped/DefinitelyTyped
I am also planning to submit the type file to https://github.com/DefinitelyTyped/DefinitelyTyped
Neat, any idea on ETA? :)
I am also planning to submit the type file to https://github.com/DefinitelyTyped/DefinitelyTyped
Neat, any idea on ETA? :)
I have submitted a PR here, waiting for review: https://github.com/DefinitelyTyped/DefinitelyTyped/pull/43165
npm install @types/browser-image-compression
doesn't install any types because there aren't any. I had to add an empty definitions file just to use it. Please add some types