New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve typescript types #390
Conversation
} | ||
|
||
export interface MultipartValue<T = unknown> { | ||
value: T; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The value should be unknown
, since it might be parsed as JSON based on the content-type provided by the caller. I've left it generic in case someone wants to assert its type.
export interface MultipartValue<T = unknown> { | ||
value: T; | ||
fieldname: string; | ||
mimetype?: string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
string | undefined
because mime type get swallowed when we parse the value as JSON. I think this is a bug, but that's for another PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Our working code wasn't valid after this: Adding:
according to those unit tests this became valid but it wasn't at all obvious based on types. Don't think the README example: https://github.com/fastify/fastify-multipart#handle-multiple-file-streams-and-fields
will work anymore because part.file isn't available unless you do 'file' in part so IMO should revisit this. |
@@ -109,10 +123,10 @@ const runServer = async () => { | |||
app.post('/upload/raw/any', async function (req, reply) { | |||
const parts = req.parts() | |||
for await (const part of parts) { | |||
if (part.file) { | |||
if ('file' in part) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be modified on README as well. But this needs to be revisited to be on the type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your problem is TypeScript
issue.
TypeScript
cannot restrict the type in certain format that's the cause.
The README
example is certainly valid in Javascript
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, typescript wants you to use in
for narrowing in this situation, which works for both JS and TS.
https://www.typescriptlang.org/docs/handbook/2/narrowing.html#the-in-operator-narrowing
A discriminator would perhaps be ideal:
export interface MultipartFile {
type: 'file',
...
}
export interface MultipartValue<T = unknown> {
type: 'value',
...
}
if (part.type === 'file') {
part.toBuffer()
}
I've tried to correct the types by analyzing the source code and busboy's type definitions.
Fixes #251
Checklist
npm run test
andnpm run benchmark
and the Code of conduct