Donaldcwl / browser-image-compression

Image compression in web browser
MIT License
1.31k stars 161 forks source link

no typescript definitions #37

Closed Seanmclem closed 4 years ago

Seanmclem commented 4 years ago

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

vianneyguesdon commented 4 years ago

+1

vianneyguesdon commented 4 years ago

declare module 'browser-image-compression' { interface Options { maxSizeMB: number; maxWidthOrHeight: number; useWebWorker: boolean; } function imageCompression(image: Blob, options: Options): Blob; export = imageCompression; }

Donaldcwl commented 4 years ago

Thanks. I have added the type file in the project and released in version v1.0.7.

pzi commented 4 years ago

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.

pzi commented 4 years ago

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.

Donaldcwl commented 4 years ago

Thanks @pzi, I am preparing it in the dev branch

Donaldcwl commented 4 years ago

I am also planning to submit the type file to https://github.com/DefinitelyTyped/DefinitelyTyped

pzi commented 4 years ago

I am also planning to submit the type file to https://github.com/DefinitelyTyped/DefinitelyTyped

Neat, any idea on ETA? :)

Donaldcwl commented 4 years ago

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