Skip to content

$fetch return type is any if generics are passed to event handler #3507

@dword-design

Description

@dword-design

Environment

Node: 22.16.0
Nitro: 2.12.4

Reproduction

https://github.com/dword-design/demo-nitro-fetch-type-with-generics

Describe the bug

When I have an event handler, and call that one via $fetch, normally $fetch would detect the event handler type:

Image

But when I pass a generic type param to the event handler (e.g. for query or body type), it will only have any as a type.

// server/api/foo.get.ts

export default eventHandler<{ query: { foo: string } }>(() => 'foo');
Image

It looks like it will take the default Response any type if a generic is passed and otherwise take the type from the actual result.

I was asking myself, is it recommended to pass that type or should sth. like zod be used instead?

Additional context

No response

Logs

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions