Skip to content

fix(types): add optional value to MultipartFile for onFile - #624

Open
imharjot wants to merge 3 commits into
fastify:mainfrom
imharjot:fix/multipartfile-value-type
Open

fix(types): add optional value to MultipartFile for onFile#624
imharjot wants to merge 3 commits into
fastify:mainfrom
imharjot:fix/multipartfile-value-type

Conversation

@imharjot

@imharjot imharjot commented Jun 17, 2026

Copy link
Copy Markdown

The README documents setting part.value inside an onFile handler when using
attachFieldsToBody: 'keyValues', but MultipartFile has no value property, so that
example fails to typecheck with TS2339 (Property 'value' does not exist on type 'MultipartFile').

Adds an optional value?: unknown to MultipartFile. unknown (not any) keeps it
type-safe and matches the existing Record<string, unknown> usage in this file; it's
optional because a file part only gets a value when one is assigned in onFile. Kept
MultipartFile non-generic to avoid rippling generics through Multipart/MultipartFields.

The existing type test asserted req.body.file had no value, so that assertion is flipped
and the onFile test handler now assigns part.value to cover the regression.

Fixes #574

@Tony133
Tony133 requested a review from a team July 16, 2026 10:39

@is2ei is2ei left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the issue is a bit more complicated.

It looks like assigning the value field to the part object in the onFile function is a bit of a hack.
This works because the if statement uses field.value when it is not undefined.

https://github.com/fastify/fastify-multipart/blob/main/index.js#L100-L103

            if (field.value !== undefined) {
              body[key] = field.value
            } else if (field._buf) {
              body[key] = field._buf

However, the value property is intended for objects whose type is field.

https://github.com/fastify/fastify-multipart/blob/main/index.js#L338-L347

      const value = {
        type: 'field',
        fieldname: name,
        mimetype: contentType,
        encoding,
        value: fieldValue,
        fieldnameTruncated,
        valueTruncated,
        fields: body
      }

On the other hand, the _buf property is intended for objects whose type is file.

https://github.com/fastify/fastify-multipart/blob/main/index.js#L373-L382

      const value = {
        type: 'file',
        fieldname: name,
        filename,
        encoding,
        mimetype,
        file,
        fields: body,
        _buf: null,
        async toBuffer () {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Type value does not exist on MultipartFile when using onFile

3 participants