node-formidable / formidable

The most used, flexible, fast and streaming parser for multipart form data. Supports uploading to serverless environments, AWS S3, Azure, GCP or the filesystem. Used in production.
MIT License
7k stars 680 forks source link

Typings are incomplete #922

Closed redevill closed 1 year ago

redevill commented 1 year ago

Context

What are you trying to achieve or the steps to reproduce?

Using a formidable form to upload images. The upgraded version does not appear to match the given typings.

e.g. During run time a form.parse callback contains a File object, which in turn contains an image. It appears that we need to cast from the specific "Image" back to file, as there is no Image object that implements the File interface.

Compile Error: TS2339: Property originalFilename does not exist on type File | File[]   Property originalFilename does not exist on type File[] const form = new formidable.IncomingForm(formOpts); form.parse(req, (err: Error, fields: Fields, file: Files) => { file.image.originalFilename }

What was the result you got?

Typescript Compile error

What result did you expect?

Better typings or guidance on expected usage.

choutkamartin commented 1 year ago

This is issue on your side:

Property originalFilename does not exist on type File[]

Property originalFilename can hardly exist on a type that is an array, not an object

The third argument of the callback function is either File or File[]. Not only File as you think.

redevill commented 1 year ago

So - if you are correct - and "This issue is on your side" and the only change I made was an upgrade in version, then it stands to reason, that the "issue" is the upgrade.

I am more accustomed to seeing "Breaking changes" listed in the change log.

For those that stumble here, (breaking change V1 to V2) there is some detail in the changelog: Specifically #689 refactor: split file.name into file.newFilename and file.originalFilename (https://github.com/node-formidable/formidable/pull/689)

Additional checking to determine type when you get the call back is needed. per [choutkamartin] above.

JacobGalati commented 1 year ago

@choutkamartin I ran into the same issue and realized that when I had installed formidable the @types/formidable package that came with it was v1. When I manually added @types/formidable as a dev dependency it came in with v2 and had the correct typings for Files.