query: TypeScript errors when using exported queryOptions factory in @tanstack/react-query 5.62.8
Describe the bug
When using the factory queryOptions and exporting the result, TypeScript errors appear:
Exported variable 'shopsQuery' has or is using name 'dataTagErrorSymbol' from external module "/node_modules/@tanstack/query-core/build/legacy/hydration-DiTAi-4H" but cannot be named. (ts4023)
Exported variable 'shopsQuery' has or is using name 'dataTagSymbol' from external module "/node_modules/@tanstack/query-core/build/legacy/hydration-DiTAi-4H" but cannot be named. (ts4023)
These errors occur in every instance where I export the result of queryOptions. Removing the export resolves the issue, but that isn’t an acceptable workaround.
Your minimal, reproducible example
Steps to reproduce
- Install the latest version of @tanstack/react-query (v5.62.8).
- Create and export a variable using
queryOptionsfactory, e.g.,export const shopsQuery = queryOptions(...);. - Observe TypeScript errors as described.
Expected behavior
The queryOptions factory should allow exporting without triggering TypeScript errors.
How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
- OS [Windows, Linux]
Tanstack Query adapter
react-query
TanStack Query version
v5.62.8
TypeScript version
v5.7.2
Additional context
No response
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 22
- Comments: 17 (7 by maintainers)
Commits related to this issue
- [ business-logic ] bugfix: react-query 타입 문제 - 신규버전부터 key에 대한 추론값 이슈 * https://github.com/TanStack/query/issues/8453 — committed to pmmm114/KB-Store by pmmm114 2 years ago
- Feat: business logic (#4) [ business-logic ] chore: zustand, react-query 라이브러리 추가 - fetch 및 상태 관리를 위해 @tanstack/react-query - 전역상태관리를 위해 zustand - 데이터 불변성 유지를 위해 Immer [ business-logic ] feat: NFT 쿼... — committed to pmmm114/KB-Store by pmmm114 2 years ago
I got the same problem as well in
v5.62.8but no problem inv5.62.7.Okay so some investigation against tRPC’s new client where I have observed the issue when returning
queryOptions()dataTagSymbolanddataTagErrorSymbolare indeed exported by react-query via barrel so no issues theredataTagSymbolanddataTagErrorSymbolfrom react-query does make the error go away, comes with the need to ignore unused dependencies errors in eslint but I think this is the easiest workaround for nowI noticed the way symbols are declared is different from tRPC though, and switching over does seem to work, though I don’t really know what the difference is
https://github.com/TanStack/query/pull/8468
No error:
ChatGPT (Link to chat) informs me that the declare keyword is just a declaration without an implementation, but the tRPC way actually creates a concrete symbol which we can then draw a type declaration from. That may explain why typescript isn’t behaving as people describe, the type needs to have an implementation to be inferable via a library later - I’d love to understand this more but I don’t
Hey folks, i don’t think this is properly fixed. The output d.ts is using a symbol without being imported and thus, resulting into incorrect d.ts files
Given this input:
Here is the d.ts output:
And both symbols are not imported 😕
A minimal reproduction can be looked at https://tsplay.dev/w2O6bN
A reproduction can be looked at https://github.com/TanStack/router/pull/3078 , then running
pnpm installand thenpnpm nx run router-monorepo-react-query:devOutput per version, regarding the mono example on tanstack/router
Input:
React Query 5.62.7 output:
React Query 5.62.8 output (no d.ts at all):
That was fixed with https://github.com/TanStack/query/pull/8468
However, with react Query 5.62.9, compile dts but type check is not passing, here is the d.ts:
However, both symbol are assumed global, but aren’t defined globally
Are there any updates on this? It’s been months and numerous people have thumbs upped the issue. This has prevented us from upgrading since the problematic release.
You can see it on the ts playground in the link I’ve mentioned tsplay.dev/w2O6bN, and then go to the
d.tstab on the rightThat could be a workaround, but that dosen’t change the fact that the symbol is assumed global, without a global definition 😕
What is weird is that previous to 5.62.8, the query key is using a generic
where as post 5.62.9, the query key type is computed (instead of using the generic), resulting in wrong reference
So I don’t know what change in https://github.com/TanStack/query/pull/8394/ is making the DataDat not being used via the generic and instead resolving it, that is causing the issue here
In this case no these symbols aren’t exported by either query-core or rq/angular directly, I will play with an MR which changes this as we already have a reproduction in tRPC that I can test against
Some workarounds in the mean-time:
compilerOptions.declaration: true- setting it false should squash the error