Skip to content

Middleware response type no longer merged with endpoint response type #4600

@colinlienard

Description

@colinlienard

What version of Hono are you using?

4.11.2

What runtime/platform is your app running on? (with version if possible)

Bun 1.3.4

What steps can reproduce the bug?

import { Hono } from "hono";
import { hc } from "hono/client";
import { createMiddleware } from "hono/factory";

const middleware = createMiddleware(async (c) => {
  if (Math.random() > 0.5) {
    return;
  } else {
    return c.json({ cause: "Unauthorized" as const }, 401);
  }
});

const app = new Hono().get("/", middleware, (c) => {
  return c.json({ message: "Hello Hono!" });
});

export default app;

type AppType = typeof app;

const api = hc<AppType>("");
const response = await api.index.$get();
const json = await response.json(); // Should be `{ cause: "Unauthorized" } | { message: "Hello Hono!" }`

What is the expected behavior?

json should be typed as { cause: "Unauthorized" } | { message: "Hello Hono!" }, like in previous versions

What do you see instead?

json is typed as { message: "Hello Hono!" }

Additional information

#4598 seems to be the cause of the issue. It only occurs when the middleware returns a union type

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions