Skip to content
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

Merged
merged 2 commits into from Sep 14, 2022
Merged

Improve typescript types #390

merged 2 commits into from Sep 14, 2022

Conversation

dwickern
Copy link
Contributor

I've tried to correct the types by analyzing the source code and busboy's type definitions.

Fixes #251

Checklist

}

export interface MultipartValue<T = unknown> {
value: T;
Copy link
Contributor Author

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;
Copy link
Contributor Author

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.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina mcollina merged commit b445523 into fastify:master Sep 14, 2022
@dwickern dwickern deleted the improve-ts branch September 14, 2022 22:28
@leppaott
Copy link

Our working code wasn't valid after this:
await part.toBuffer();

Adding:

if ('file' in part) {
            imageBuffer = await part.toBuffer();

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

const parts = req.parts()
  for await (const part of parts) {
    if (part.file) {
      await pump(part.file, fs.createWriteStream(part.filename))
    } else {
      console.log(part)
    }
  }

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) {

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.

Copy link
Member

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.

Copy link
Contributor Author

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()
}

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.

Typescript data.fields returns wrong type.
4 participants