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.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Typings are incomplete

redevill opened this issue · comments

  • Which support plan is this issue covered by? (Community):
  • Currently blocking your project/work? (no):
  • Affecting a production system? (yes):

Context

  • Node.js version: V16.15.0
  • Release Line of Formidable (Current):
  • Formidable exact version: V2.0.5
  • Environment (node, browser, native, OS): node
  • Used with (popular names of modules): express

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.

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.

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 (#689)

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

@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.