react-dropzone-uploader
react-dropzone-uploader copied to clipboard
Unable to compile with Typescript 4.5.4
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)
}
Made the changes, please see it.
Thank you it's great!
@all-contributors please add @AlacritousCreature for code and bug