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:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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
Still there is no section for typescript usage 🤔