T
TanStack2y ago
fair-rose

[TS Error] Can't export useQuery result

I'm currently getting the following TS error which I can't really resolve when trying to export a useQuery result:
The inferred type of 'query' cannot be named without a reference to '../../node_modules/@tanstack/vue-query/build/modern/useQuery-CPqkvEsh'. This is likely not portable. A type annotation is necessary.ts(2742)
The inferred type of 'query' cannot be named without a reference to '../../node_modules/@tanstack/vue-query/build/modern/useQuery-CPqkvEsh'. This is likely not portable. A type annotation is necessary.ts(2742)
Code:
import { useQuery } from "@tanstack/vue-query";

export const query = useQuery({
queryKey: ["test"],
queryFn: () => Promise.resolve(5),
});
import { useQuery } from "@tanstack/vue-query";

export const query = useQuery({
queryKey: ["test"],
queryFn: () => Promise.resolve(5),
});
The only workaround I found is to change the code the following way (But this isn't really a solution for me):
import * as VueQuery from "@tanstack/vue-query";

export const query = VueQuery.useQuery({
queryKey: ["test"],
queryFn: () => Promise.resolve(5),
});
import * as VueQuery from "@tanstack/vue-query";

export const query = VueQuery.useQuery({
queryKey: ["test"],
queryFn: () => Promise.resolve(5),
});
A common workaround for this issue is also to set the compilerOptions.declaration option in the tsconfig.json to false. But I can't do this as I need the declaration files. Here are also some informations I found about this error: https://github.com/microsoft/TypeScript/issues/47663#issuecomment-1519138189 Can someone help me on how to properly fix this issue or is this maybe also a bug in the library?
GitHub
"The inferred type of X cannot be named without a reference to Y" (...
Bug Report 🔎 Search Terms TS2742 cannot be named without a reference 🕗 Version & Regression Information Problems occurs with 4.5.x and 4.6.x and most likely earlier versions (I've tried a f...
1 Reply
fair-rose
fair-roseOP2y ago
Just saw that there is actually already a GH issue about this. So I guess this can be resolved https://github.com/TanStack/query/issues/6318
GitHub
[vue-query]: Fresh Vue3 project + Tanstack - Typescript errors, Thi...
Describe the bug With a fresh Vue3 installation with @tanstack/vue-query installed - VSCode produces typescript errors, as seen in the screenshot. It's not clear to me if this is an issue with ...

Did you find this page helpful?