-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeScript errors when using exported queryOptions factory in @tanstack/react-query 5.62.8 #8453
Comments
I got the same problem as well in |
@TkDodo @Nick-Lucas I'm getting comments about typing problems since v5.62.7 / #8394 on Angular too, e.g.: #6293 (comment) |
I've seen this too in the tRPC codebase, returning a datatagged thing (query key, query options, etc) shows a portability error unless you explicitly type the return - as a workaround that's the solution I'm not entirely sure right now how to prevent it but I know it's a common problem in libs @juliusmarminge we've solved this for tRPC before, right? |
Did you create some new types that aren't exported from a public entrypoint? That's usually a primary cause. But RQ is, and never will be with the current behaviors in TypeScript, not fully portable since it relies on |
Interestingly Perhaps the root is is simply that |
Are all types used for data tags and queryOptions exported from the main RQ entrypoint? Are any re-exports from core package? EDIT: Ah the symbols are from core package. Can they not be defined and exported in the framework package? I think that might help TS discover them more easily. IIRC last time I looked at queryOptions stuff all types are in each framework package so why can't the data tags also be there? |
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:
|
definitely need
we can export the symbols from the core, no problem.
no, because core things in the |
@Nick-Lucas as far as I can see, we do export the symbols: query/packages/query-core/src/types.ts Lines 45 to 47 in 4989399
query/packages/query-core/src/index.ts Lines 34 to 35 in cd68280
I can also import them from |
same here |
Yes that's what I want to ensure is happening, hard to trace through barrel files though, back online now so I'll take a look |
Okay so some investigation against tRPC's new client where I have observed the issue when returning
I 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 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: import { queryOptions } from "@tanstack/react-query"
export const exported = queryOptions({
queryKey: ['invalid'],
}) Here is the d.ts output: export declare const exported: import("@tanstack/react-query").OmitKeyof<import("@tanstack/react-query").UseQueryOptions<unknown, Error, unknown, string[]>, "queryFn"> & {
queryFn?: import("@tanstack/react-query").QueryFunction<unknown, string[], never> | undefined;
} & {
queryKey: string[] & {
[dataTagSymbol]: unknown;
[dataTagErrorSymbol]: Error;
};
}; And both symbols are not imported :/ A minimal reproduction can be looked at https://tsplay.dev/w2O6bN A reproduction can be looked at TanStack/router#3078 , then running Output per version, regarding the mono example on tanstack/routerInput: import { queryOptions } from '@tanstack/react-query'
import { fetchPost } from './posts'
export const postQueryOptions = (postId: string) =>
queryOptions({
queryKey: ['posts', { postId }],
queryFn: () => fetchPost(postId),
}) React Query 5.62.7 output: export declare const postQueryOptions: (postId: string) => import('@tanstack/react-query').OmitKeyof<import('@tanstack/react-query').UseQueryOptions<import('./posts').PostType, Error, import('./posts').PostType, import('@tanstack/react-query').QueryKey>, "queryFn"> & {
queryFn?: import('@tanstack/react-query').QueryFunction<import('./posts').PostType, import('@tanstack/react-query').QueryKey, never> | undefined;
} & {
queryKey: import('@tanstack/react-query').DataTag<import('@tanstack/react-query').QueryKey, import('./posts').PostType>;
}; React Query 5.62.8 output (no d.ts at all): src/postQueryOptions.tsx:4:14 - error TS4023: Exported variable 'postQueryOptions' has or is using name 'dataTagErrorSymbol' from external module "/Users/nicolasbeaussart-hatchuel/work/perso/git/tanstack/router/node_modules/.pnpm/@[email protected]/node_modules/@tanstack/query-core/build/modern/hydration-DiTAi-4H" but cannot be named.
4 export const postQueryOptions = (postId: string) =>
~~~~~~~~~~~~~~~~
src/postQueryOptions.tsx:4:14 - error TS4023: Exported variable 'postQueryOptions' has or is using name 'dataTagSymbol' from external module "/Users/nicolasbeaussart-hatchuel/work/perso/git/tanstack/router/node_modules/.pnpm/@[email protected]/node_modules/@tanstack/query-core/build/modern/hydration-DiTAi-4H" but cannot be named.
4 export const postQueryOptions = (postId: string) => That was fixed with #8468 However, with react Query 5.62.9, compile dts but type check is not passing, here is the d.ts: export declare const postQueryOptions: (postId: string) => import('@tanstack/react-query').OmitKeyof<import('@tanstack/react-query').UseQueryOptions<import('./posts').PostType, Error, import('./posts').PostType, (string | {
postId: string;
})[]>, "queryFn"> & {
queryFn?: import('@tanstack/react-query').QueryFunction<import('./posts').PostType, (string | {
postId: string;
})[], never> | undefined;
} & {
queryKey: (string | {
postId: string;
})[] & {
[dataTagSymbol]: import('./posts').PostType;
[dataTagErrorSymbol]: Error;
};
}; However, both symbol are assumed global, but aren't defined globally |
That's really unusual and I'm not sure what could be wrong here Couldn't get the tsplayground to reproduce, but the router PR is definitely not behaving how I would expect
My best idea was the types needed explicitly exporting from query-core and react-query, but that hasn't helped: https://github.com/TanStack/query/pull/8481/files tsconfig's |
Describe the bug
When using the factory
queryOptions
and exporting the result, TypeScript errors appear: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
https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAbzgRwK4FMoE8DyYbAQB2AznAL5wBmUEIcARAAIwCGpbAxgNYD0U6VpxgBaNJiwMAsAChZnYiXgKoA4QEUM2PAUVwAvCi258hUgAoEsuEYkBpdFgBccANoByFWpjuAugBpZcgBKWTCZdAAPSFg4BQ44YCIAN1YAG2AAE00JHTMyQ3FtU0VLa1tsB2c3dyTUjMy-QJkQoA
Steps to reproduce
queryOptions
factory, e.g.,export const shopsQuery = queryOptions(...);
.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
Tanstack Query adapter
react-query
TanStack Query version
v5.62.8
TypeScript version
v5.7.2
Additional context
No response
The text was updated successfully, but these errors were encountered: