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

Incorrect value for inferred type from Eden treaty when returning FormData #989

Open
aryzing opened this issue Jan 3, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@aryzing
Copy link

aryzing commented Jan 3, 2025

What version of Elysia is running?

1.2.9

What platform is your computer?

Linux 6.8.0-50-generic x86_64 x86_64

What steps can reproduce the bug?

From a handler, return FormData like so:

  .get("/example", async () => {
    const file = new File(["hello world"], "hello.txt", { type: "text/plain" });
    
    const formData = new FormData();
    formData.append("file", file);

    return formData;
  })

What is the expected behavior?

If the inferred type of the response FormData, then the value should be a FormData instance.

What do you see instead?

The value is not a FormData instance. Seems Elysia unwraps the FormData and provides a value that looks like

{
  file: File
}

but the entire object is typed as FormData, which doesn't match the actual value.

Additional information

When returning { file } from the handler, Elysia infers the type as { file: File } which doesn't match the actual value either. It's just an empty object on the client, {}.

Seems the only way to preserve file data on the client and getting the correct types is by returning FormData from the handler, but forcing the correct type on the client,

const file = (res as unknown as { file: File }).file;
@aryzing aryzing added the bug Something isn't working label Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant