Replies: 1 comment
-
|
The short answer is: Hono deliberately mirrors the Web Platform's own Why the Web Platform returns
|
Beta Was this translation helpful? Give feedback.
-
|
The short answer is: Hono deliberately mirrors the Web Platform's own Why the Web Platform returns
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
This is a genuine question, not a critique. Hono has been a delight to use and has already saved me days of work - but still curious;
By default await
c.req.json()returns typeany. I understand this aligns withJSON.parse()but I was always under the assumptionJSON.parse()returninganyinstead ofunknownwas becauseunknownwas added to the TypeScript language years afteranyand at that point due to backward compatibility it could never be changed. That was a very long time ago however.Hono is a relatively recent framework. The focus on web-standards and being a micro-framework seems to align (intentionally or unintentionally) with the general JS ecosystem shift away from vulnerable, magical and bloated dependencies to more secure, explicit and thin dependencies. Alongside the security focused shift in libraries there is a parallel shift in TypeScript towards more and more strictly typed projects.
The theories of "Type Driven Design", "parse, don't validate" and "boundary validation" have been around for a while. The
c.req.json()function I imagine is likely a very heavily used function and is is likely the boundary entry-point to many projects. Just seems odd to me that the entry-point of many projects disables typing.I understand the function can take a generic (
c.req.json<MyDto>()) but that is just type assertion which can be completely incorrect to the runtime value. Likewise I know there are some built in features likezodvalidation (c.req.valid()) but I'm talking about the situation where a developer may not want to use that and do their own validation for whatever reason.Hono looks to be poised to be the framework of choice for modern TS projects wanting to move away from the supply-chain-attack mess currently unfolding, but this seems like a bit of a miss in relation to type safety so I wonder what I am missing or what the logic was for returning
anyin this particular case?I know that code review, custom lint rules etc etc could work-around this, but that is still a workaround - educating junior developers is difficult enough without the argument of "if unknown is more accurate than any then why would a modern framework return any?"
I did stumble across this thread, but again, its a workaround and I don't see reasoning;
https://github.com/orgs/honojs/discussions/3331
Beta Was this translation helpful? Give feedback.
All reactions