hono icon indicating copy to clipboard operation
hono copied to clipboard

c.render with jsxRenderer has type errors for 2 arguments

Open janat08 opened this issue 1 year ago • 6 comments

What version of Hono are you using?

4.2.7

What runtime/platform is your app running on?

Workers

What steps can reproduce the bug?

export const Layout: FC = ({ children, c, title }) => { app.get("*", jsxRenderer(Layout)); return c.render(index(c), { c, title: "Home", search: true }); Expected 1 arguments, but got 2

What is the expected behavior?

No response

What do you see instead?

No response

Additional information

No response

janat08 avatar May 03 '24 12:05 janat08

Argument of type '({ children, title }: PropsWithChildren<{ Layout: FC; }>) => string' is not assignable to parameter of type 'ComponentWithChildren'. Type 'string' is not assignable to type 'HtmlEscapedString | Promise<HtmlEscapedString>'

If just using the example from hono docs

janat08 avatar May 03 '24 12:05 janat08

Also what exactly is suppose to be the type of layout component that isn't defined inline?

janat08 avatar May 03 '24 12:05 janat08

Hi @janat08

Please share the minimal project to reproduce it.

yusukebe avatar May 03 '24 15:05 yusukebe

https://github.com/janat08/reproduction-hono

janat08 avatar May 03 '24 16:05 janat08

@janat08

Thanks! Please add the following to src/index.ts or global.d.ts.

declare module 'hono' {
  interface ContextRenderer {
    (content: string | Promise<string>, props: { title: string }): Response
  }
}

yusukebe avatar May 03 '24 16:05 yusukebe

what type is layout function then?

janat08 avatar May 03 '24 17:05 janat08

@janat08

How about using PropsWithChildren?

import type { PropsWithChildren } from 'hono/jsx'

export const Layout = ({ children, title }: PropsWithChildren<{ title: string }>) => {
  return (
    <html>
      <head>
        <meta charset="utf-8" />
        <link rel="manifest" href="/static/manifest.json" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>{title}</title>
      </head>
      <body>
        <main class="responsive" role="main">
          {children}
        </main>
      </body>
    </html>
  )
}

yusukebe avatar May 04 '24 00:05 yusukebe

The reproduction went back to breaking.

janat08 avatar Jun 04 '24 04:06 janat08

https://hono.dev/middleware/builtin/jsx-renderer#extending-contextrenderer

janat08 avatar Jun 04 '24 05:06 janat08

@janat08

I can't understand where the error occurs. Could you provide a minimal project to reproduce it again?

yusukebe avatar Jun 05 '24 18:06 yusukebe

https://github.com/janat08/reproduction-hono

janat08 avatar Jun 06 '24 06:06 janat08

@janat08 Thanks!

Try to use this code:

import { Hono } from 'hono'
import { jsxRenderer } from 'hono/jsx-renderer'
import { Layout } from './layoutBeercss'

const app = new Hono()

app.get('*', jsxRenderer(Layout))

declare module 'hono' {
  interface ContextRenderer {
    (content: string | Promise<string>, props: { title: string }): Response
  }
}

// ...

yusukebe avatar Jun 06 '24 08:06 yusukebe

That was eslint changing the name over a ruleset for abbreviations.

janat08 avatar Jun 06 '24 11:06 janat08