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

fix(context): update HTMLRespond type #3313

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

rossrobino
Copy link

If you pass anything with a toString method into c.html it will return the result, but the types don't currently reflect this.

This PR adds a HasToString type as an option for the first argument of c.html.

This enables you to do things like this and not have TS errors. Right now this works but TS errors.

const app = new Hono();

app.get("/number", (c) => c.html(1));

const obj = { toString: () => "hello world" };

app.get("/hello", (c) => c.html(obj));
No overload matches this call.
  Overload 1 of 2, '(html: string | Promise<string>, status?: StatusCode | undefined, headers?: HeaderRecord | undefined): Response | Promise<Response>', gave the following error.
    Argument of type 'number' is not assignable to parameter of type 'string | Promise<string>'.
  Overload 2 of 2, '(html: string | Promise<string>, init?: ResponseInit | undefined): Response | Promise<Response>', gave the following error.
    Argument of type 'number' is not assignable to parameter of type 'string | Promise<string>'.ts(2769)

Thanks for the review, I'm not sure if there's a case I'm not considering here.

The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

@yusukebe yusukebe changed the title update HTMLRespond type fix(context): update HTMLRespond type Aug 25, 2024
@yusukebe
Copy link
Member

@rossrobino Thank you for the PR.

@usualoma Can you review this?

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

Successfully merging this pull request may close these issues.

2 participants