admin管理员组文章数量:1400420
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);
本文标签: typescriptHow to correctly address the type behind useFetch in NuxtStack Overflow
版权声明:本文标题:typescript - How to correctly address the type behind useFetch in Nuxt? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744207006a2595237.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
if (data.value?.url)
is ok – Estus Flask Commented Mar 25 at 10:20