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

v3.11.5 breaks typed routes on the client #1842

Closed
MonsterDeveloper opened this issue Dec 22, 2023 · 6 comments · Fixed by #1853
Closed

v3.11.5 breaks typed routes on the client #1842

MonsterDeveloper opened this issue Dec 22, 2023 · 6 comments · Fixed by #1853
Labels

Comments

@MonsterDeveloper
Copy link
Contributor

What version of Hono are you using?

v3.11.9

What runtime/platform is your app running on?

Bun

What steps can reproduce the bug?

Link to TS Playground

What is the expected behavior?

Client should correctly transform the type of objects.

What do you see instead?

No response

Additional information

After an update to v3.11.9, the types for Hono client's responses in my app are now broken. The bug was introduced in v3.11.5 in PR #1806

Although transforming types to JSON works fine with simple objects, once you start using nested objects and interfaces, it falls apart.

The Jsonify utility from type-fest works fine, however. In the end of the playground I showed the correct types using this utility.

I believe the fix should be easy to implement (i.e. just use the code from type-fest).

@yusukebe
Copy link
Member

Hi @MonsterDeveloper

Thanks for raising the issue. To think about this more deeply, can you show me the minimal code to reproduce your problem?

@yusukebe
Copy link
Member

@MonsterDeveloper
Copy link
Contributor Author

@yusukebe here's a link to a playground with a simple Hono app.

Please note how someMeta got turned into string for some reason, although the SampleInterface surely is losslessly jsonifiable.

CleanShot 2023-12-23 at 14 01 04

@MonsterDeveloper
Copy link
Contributor Author

It seems to me this is the issue with interfaces only. Plain types are transformed correctly.

I think this remark from type-fest's Jsonify is related to this behavior.

I'm not sure whether it is possible at all to make json transforms work with interfaces.

@yusukebe
Copy link
Member

yusukebe commented Dec 24, 2023

@MonsterDeveloper

How about using the JSONParsed as below?

export type JSONParsed<T> = T extends { toJSON(): infer J }
  ? (() => J) extends () => JSONObject
    ? J
    : JSONParsed<J>
  : T extends JSONPrimitive
  ? T
  : T extends Array<infer U>
  ? Array<JSONParsed<U>>
  : T extends object
  ? { [K in keyof T]: JSONParsed<T[K]> }
  : never

@MonsterDeveloper
Copy link
Contributor Author

Seems to be working just fine. Thanks!

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.

2 participants