readBody
has a default returnType of Promise<any>
instead of Promise<unknown>
#386
Labels
good first issue
Good for newcomers
Environment
Node v20.2.0
h3 1.6.6
typescript 5.0.4
Reproduction
Describe the bug
When using
readBody
, the output type is typed asPromise<any>
. This in turn means it's very easy to accidentally lose type safety from the response body, as TypeScript will treat any use of the response as valid (where it's most likely not going to be). By returningPromise<unknown>
instead, h3 can explicitly communicate that the return type should be handled with care, instead of treating every use of it as valid. You can find a super basic example of the type of any "trap" in this playground: https://www.typescriptlang.org/play?#code/MYewdgzgLgBAhgEwQFQO4hgXhgCjAVwFsAuGAwgIwFMAnASiwD4yiYBqGAJgG4BYAKAGhIsCiAQBPAIJgJpOLKwwARAAsqAGw0Z0NDQmV9+w6DDGSAqmADWYEKjCl8Nuw6VrN2mLv2GBAxBR0HHNpWTojQLQQEPEJK1t7MAigA. Note how the unknown body use throws a helpful error in this mistaken usage example.Additional context
You can workaround the problem by providing the explicit type you expect in the override:
or manually set
unknown
if you can't rely on the input body:Logs
No response
The text was updated successfully, but these errors were encountered: