I use the useFetch()
composable in Nuxt (3.16) and VS Code signals that one of the properties I get from the call does not exist -- this is in reality a type comparison issue I do not understand.
My call is:
const { data, error } = await useFetch(`/api/auth/login?provider=${provider}`)
data
has the following signature:
const data: globalThis.Ref<Simplify<SerializeObject<{
error: string;
url?: undefined;
success?: undefined;
user?: undefined;
token?: undefined;
}> | SerializeObject<{
url: string;
error?: undefined;
success?: undefined;
user?: undefined;
token?: undefined;
}> | SerializeObject<...> | SerializeObject<...> | SerializeObject<...>> | null, Simplify<...> | null>
Further on I am accessing the url
property:
if (data.value?.url) {
window.location.href = data.value.url
}
url
has a wiggle underneath and TS signals that
Property 'url' does not exist on type 'Simplify<SerializeObject<{ error: string; url?: undefined; success?: undefined; user?: undefined; token?: undefined; }> | SerializeObject<{ url: string; error?: undefined; success?: undefined; user?: undefined; token?: undefined; }> | SerializeObject<...> | SerializeObject<...> | SerializeObject<...>>'.
Property 'url' does not exist on type '{ error: string; }'.ts(2339)
While the code works fine, I would like to cleanly fix this error and use the opportunity to understand how to compare such types.
I use the useFetch()
composable in Nuxt (3.16) and VS Code signals that one of the properties I get from the call does not exist -- this is in reality a type comparison issue I do not understand.
My call is:
const { data, error } = await useFetch(`/api/auth/login?provider=${provider}`)
data
has the following signature:
const data: globalThis.Ref<Simplify<SerializeObject<{
error: string;
url?: undefined;
success?: undefined;
user?: undefined;
token?: undefined;
}> | SerializeObject<{
url: string;
error?: undefined;
success?: undefined;
user?: undefined;
token?: undefined;
}> | SerializeObject<...> | SerializeObject<...> | SerializeObject<...>> | null, Simplify<...> | null>
Further on I am accessing the url
property:
if (data.value?.url) {
window.location.href = data.value.url
}
url
has a wiggle underneath and TS signals that
Property 'url' does not exist on type 'Simplify<SerializeObject<{ error: string; url?: undefined; success?: undefined; user?: undefined; token?: undefined; }> | SerializeObject<{ url: string; error?: undefined; success?: undefined; user?: undefined; token?: undefined; }> | SerializeObject<...> | SerializeObject<...> | SerializeObject<...>>'.
Property 'url' does not exist on type '{ error: string; }'.ts(2339)
While the code works fine, I would like to cleanly fix this error and use the opportunity to understand how to compare such types.
Share Improve this question edited Mar 25 at 9:31 WoJ asked Mar 25 at 9:24 WoJWoJ 30.1k58 gold badges214 silver badges405 bronze badges 1 |1 Answer
Reset to default 1You need to pass types to useFetch
because it has generic type inference for data
. If you do this, there will be no error:
const { data } = await useFetch<{
error: string;
url?: undefined;
success?: undefined;
user?: undefined;
token?: undefined;
}>("/api/auth/", {
query: { provider: "google" },
});
console.log(data.value?.url);
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744207007a4563152.html
if (data.value?.url)
is ok – Estus Flask Commented Mar 25 at 10:20