Extracting `useFetch` return type
Is there a better way of extracting the type my server route is returning from a route in vue side of the app? When I do
nuxt is aware of what my server route returns and correctly types the
data
. But I want to get the type a route returns somewhere else. For example to be used in a component defineProps
if a parent was doing the fetching and component is expecting to receive it
without having to manually retype it. I got to
and it works I guess but properties of Tmp
are all wrapped in SerializeObject<>
, like
Everything still works, Tmp['meta']
will be Record<string, string>[]
but I'm wondering if there's a simpler way2 Replies
Hi @asasinmode. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Based on the knowledge sources provided, there isn't a straightforward or officially documented way to extract the exact return type from a
useFetch
call in Nuxt 3 without the SerializeObject
wrapper. However, I can provide some insights and a potential workaround based on the information available.
The SerializeObject
wrapper is likely related to how Nuxt handles serialization of data between the server and client. As mentioned in the Data Fetching documentation, Nuxt uses devalue
for serialization, which allows for transferring more complex data types.
A potential workaround to get a cleaner type without the SerializeObject
wrapper could be to use a custom type assertion. Here's an example of how you might do this:
This approach creates a utility type UnwrappedApiResponse
that removes the SerializeObject
wrapper, and then applies it to each property of the original response type.
It's worth noting that this is not an official solution, and it may not work perfectly in all cases. The Nuxt team is aware of type-related issues and is working on improvements. For instance, there's ongoing work to improve type safety in routing, as mentioned in this YouTube video on type-safe routing in Nuxt.
For the most up-to-date and accurate information on handling types with useFetch
, it's recommended to check the latest Nuxt documentation or ask in the Nuxt community forums.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@282178759204012032> Kapa.ai is still learning and improving, please let me know how I did by reacting below.