useFetch adds null to return type
When using
useFetch
to fetch data from server api routes, useFetch
adds null
as a union to the return type.
Because I use typescript strict mode, this forces me to do null checks to api routes which already handle this.
If I understand correctly, when using SSR, useFetch
is just an async function call to the api endpoints?
Then why does it add null to the type?
If this works as intended, how would I check if the data is null inside script setup
![No description](https://cdn.answeroverflow.com/1056617463468535818/image.png)
![No description](https://cdn.answeroverflow.com/1056617463825059870/image.png)
7 Replies
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
A similar problem goes for returning a
Date
. It becomes a string
(because json
doesn't support Date
?)
But typescript still thinks it is a Date
which is quite annoying.
Are these known issues? Maybe add some transform into the useFetch
composable?Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
The return type changes from
Ref<regattas[] | null>
to Ref<SerializeObject<UndefinedToOptional<regattas[]>> | null>
so something still adds null
to the type.Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Ok, I thought when using SSR this wouldn't be a problem. So how would I check the data for
null
? The example given at the top doesn't do anything. Do I need to wrap it in a computed
or anything?Unknown User•2y ago
Message Not Public
Sign In & Join Server To View