question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inconsistent Multipart type when some fields are not files

See original GitHub issue

🐛 Bug Report

When sending multiple fields in the form-data, Typescript types them all as files, even if it’s not the case

To Reproduce

Steps to reproduce the behavior:

server.post<{Body: {field1: Multipart, field2:Multipart}}>(...)
http -f :3000/mulitpart field1=foo field2@myFile.txt

With this call, we submit a file and a string. But due to the Multipart type, they’re all typed the same and we can’t access the .value property containing the string (as in this test).

Expected behavior

We could take advantage of TypeScript conditional typing to allow user to properly type its body.

interface MultipartFile {
    filename: string;
}
interface MultipartString {
    value: string;
}
type Multipart<T = true> = T extends string ? MultipartString : MultipartFile;

const mult = {} as Multipart<string>
mult.value // OK
mult.filename // error

const multFile = {} as Multipart
multFile.value // error
multFile.filename // OK

Playground to see it in action

Your Environment

  • node version: 14
  • TypeScript version: 4.0.2
  • fastify version: 3.3.0
  • fastiify-multipart version: 3.2.0
  • os: Linux

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
StarpTechcommented, Apr 27, 2021

Definitly. We could create a section called “Typescript usage” and link it to https://github.com/fastify/fastify-multipart/blob/master/test/index.test-d.ts

1reaction
mage1k99commented, Sep 30, 2022

Still there is no section for typescript usage 🤔

Read more comments on GitHub >

github_iconTop Results From Across the Web

Inconsistent response using input type="File ... - Stack Overflow
This issue appears to be resolved. We had increased the size of UploadReadAheadSize on the server to 200KB, which had no effect. So...
Read more >
Inconsistency with "multipart/form-data" and "name" field - Bugs
I have a source system that send's their data without the name field. The RFC 2388 says that it "expects" a name, but...
Read more >
RFC 1867: Form-based File Upload in HTML
In such a case, the CGI program would note that the content-type is multipart/form-data, parse the various fields (checking for validity, writing the...
Read more >
<input type="file"> - HTML: HyperText Markup Language | MDN
A file input's value attribute contains a string that represents the path to the selected file(s). If no file is selected yet, ...
Read more >
Database Engine events and errors - SQL Server
402, 16, No, The data types %s and %s are incompatible in the %s operator. ... ls' cannot be started because some of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found