react-dropzone-uploader icon indicating copy to clipboard operation
react-dropzone-uploader copied to clipboard

Unable to compile with Typescript 4.5.4

Open GSuggitt opened this issue 3 years ago • 4 comments

I recently upgraded the version of typescript in a react project to version 4.5.4. When I did this I got an compile error reported in react-dropzone-uploader. I am using version 2.11.0 of react-dropzone-uploader In Typescript 4.4 the typing of the catch variable was changed to be "unknown" from "any". As a result the error was reported. The error I get is: node_modules/react-dropzone-uploader/dist/Dropzone.tsx:504:44 - error TS2571: Object is of type 'unknown'. The code in Dropzone.tsx is:

  uploadFile = async (fileWithMeta: IFileWithMeta) => {
    const { getUploadParams } = this.props
    if (!getUploadParams) return
    let params: IUploadParams | null = null
    try {
      params = await getUploadParams(fileWithMeta)
    } catch (e) {
      console.error('Error Upload Params', e.stack)
    }
    if (params === null) return
    const { url, method = 'POST', body, fields = {}, headers = {}, meta: extraMeta = {} } = params
    delete extraMeta.status

The problem is the catch variable "e" is now typed as "unknown" and so it is an error to attempt to access a member of "unknown"

Typescript has introduced an option "useUnknownInCatchVariables" which when set to "false" will allow the code to compile and that is how I am getting around the problem.

A suggestion has been made to me that I need to wrap my call to react-dropzone-uploader in a try-catch to solve my problem. That will not solve my issue. My issue is I cannot compile my project. The person making the suggestion seemed to believe that the try-catch in question was in my code and that I should fix my code.

The problem lies in the Dropzone.tsx file of react-dropzone_uploader. And the fix is very easy:

    try {
      params = await getUploadParams(fileWithMeta)
    } catch (e) {
      console.error('Error Upload Params', (e as any).stack)
    }

GSuggitt avatar Jan 13 '22 15:01 GSuggitt

Made the changes, please see it.

AlacritousCreature avatar Sep 24 '20 18:09 AlacritousCreature

Thank you it's great!

@all-contributors please add @AlacritousCreature for code and bug

shahednasser avatar Sep 24 '20 19:09 shahednasser

@shahednasser

I've put up a pull request to add @AlacritousCreature! :tada:

allcontributors[bot] avatar Sep 24 '20 19:09 allcontributors[bot]