Fetch is typed but not the object i put it in
When getting the /api/test it is typed correctly.
Using it below
But if i ever try to use storeData it complains that it is of type any, how can i get the type the fetch gives and put as type for storeData?
2 Replies
Hi @Toffe. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
The issue you're facing is that TypeScript isn't inferring the type from your
$fetch
call to your storeData
ref. This is a common typing challenge when working with Nuxt's data fetching utilities.
Here's how you can properly type your store data:
Alternatively, you can also explicitly define your response type:
Note that when working with Nuxt's data fetching, you need to be aware of the serialization that happens between server and client. As mentioned in the Nuxt documentation on data fetching, data returned from API routes is serialized using JSON.stringify
, which means complex types like Date
objects will be converted to strings. Nuxt tries to convert the return type to match the actual value, but you might need to handle this conversion manually in some cases.
If you're using useFetch
instead of $fetch
, you would need to handle the fact that the data is wrapped in a ref:
Remember that the data
from useFetch
is a ref that needs to be accessed with .value
as explained in the useFetch documentation.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@260145305754927104> Kapa.ai is still learning and improving, please let me know how I did by reacting below.