NuxtN
Nuxt10mo ago
6 replies
ThoorAdam

useFetch type inference

Hello!
Been using nuxt for a few days now and most things are amazing so far, love the overall DX. I do have one small gripe with useFetch though.


Take the following code
const { data: recipe } = await useFetch(`/api/recipes/${id}`);


This seems to infer the types, at least somewhat, but it leads to strange linting errors not letting me access the values of the recipe.

However if I do the following and use a generic it does work as i expect it to.
const { data: recipe } = await useFetch<Recipe>(`/api/recipes/${id}`);


Do I really need to use generics everytime I fetch data to get the correct type inference? Don't get me wrong, it's not the biggest hassle, until I change what data the route returns for example. Then it could mean changing the generic in a lot of places.
Was this page helpful?