Type safety in middleware?

Hi, it's there a way to get the types right here?
import { Hono } from "hono"
import { hc } from "hono/client"

export const honoApp = new Hono()
  .use(async (c, next) => {
    await next()
    if (Math.random() > 0.5) {
      return c.json({ authenticated: false }, 401)
    }
  })
  .get("/", (c) => {
    if (Math.random() > 0.5) {
      return c.json({ message: "ok" }, 200)
    }
    return c.json({ error: "internal error" }, 500)
  })

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _honoClient = hc<typeof honoApp>("/")
type HonoClient = typeof _honoClient

const createHonoClient = (...args: Parameters<typeof hc>): HonoClient => hc<typeof honoApp>(...args)

export const honoClient = createHonoClient("/")
honoClient.index.$get().then(async (res) => {
  const data = await res.json()
  console.log(data)
})


I expected data to be
const data: {
    message: string;
} | {
    error: string;
} | {
    authenticated: boolean;
}


But actually it is
const data: {
    message: string;
} | {
    error: string;
}
Was this page helpful?