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

With typescript, type inference is lost if the json returned is not an object but an array #2878

Closed
bosens-China opened this issue May 31, 2024 · 4 comments · Fixed by #2920
Labels

Comments

@bosens-China
Copy link

What version of Hono are you using?

4.3.11"

What runtime/platform is your app running on?

node

What steps can reproduce the bug?

const app = new Hono()
  .get("/", async (c) => {
    const data = await readData();
    return c.json(data);
  })
  .get(
    "/filtration",
    zValidator(
      "query",
      z.object({
        pageSize: z.number().default(10),
      })
    ),
    async (c) => {
      const { pageSize } = c.req.valid("query");
      const data = await readData();
      const result = await splitArrayBySize(data, pageSize);
      return c.json(result);
    }
  );

export default app;
  client.api.issues.filtration.$get({ query: {} }).then((res) => {
    const value = res.json();
// never
  });

What is the expected behavior?

Hopefully, the result will be returned correctly

What do you see instead?

No response

Additional information

If you return objects instead, everything is fine

const app = new Hono()
  .get("/", async (c) => {
    const data = await readData();
    return c.json(data);
  })
  .get(
    "/filtration",
    zValidator(
      "query",
      z.object({
        pageSize: z.number().default(10),
      })
    ),
    async (c) => {
      const { pageSize } = c.req.valid("query");
      const data = await readData();
      const result = await splitArrayBySize(data, pageSize);
      return c.json({result});
    }
  );

export default app;
@yusukebe
Copy link
Member

Hi, @bosens-China

I can't reproduce it. Could you provide the minimal project to reproduce it?

@bosens-China
Copy link
Author

@yusukebe

bug like

@yusukebe
Copy link
Member

yusukebe commented Jun 6, 2024

@bosens-China Thank you!

This problem caused by result type - [number, IssuesDaum[]][] will be treated by never:

CleanShot 2024-06-06 at 18 00 10@2x

CleanShot 2024-06-06 at 18 01 22@2x

@NamesMT What do you think of this matter? [number, IssuesDaum[]][] should be valid JSON type?

@NamesMT
Copy link
Contributor

NamesMT commented Jun 6, 2024

Hi @yusukebe, yes it should be valid, I will make a PR soon fixing the newly added type, it wasn't actually "deep", only one level traverse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants