Refactored the `getQueryData` method in `QueryClient` to use generics for type inference instead of type assertion with `as`.
The previous implementation of `getQueryData` relied on a type assertion (`as`) to cast the result of `queryCache.get()`:
```typescript
return this.#queryCache.get(options.queryHash)?.state.data as
| TInferredQueryFnData
| undefined
```
While this works, using generics is a more type-safe and idiomatic approach in TypeScript. It allows the type system to infer the correct type for `TInferredQueryFnData` when `queryCache.get` is called, reducing the need for manual type casting and potential runtime errors if the cast is incorrect.
## Changes
The `getQueryData` method has been updated to pass `TInferredQueryFnData` as a generic type argument to `this.#queryCache.get()`:
**Before:**
```typescript
return this.#queryCache.get(options.queryHash)?.state.data as
| TInferredQueryFnData
| undefined
```
**After:**
```typescript
return this.#queryCache.get<TInferredQueryFnData>(options.queryHash)?.state
.data
```
This change leverages the existing generic type parameter `TInferredQueryFnData` already defined in the `getQueryData` signature and `QueryCache#get` method.
This refactor extracts the creation of the query function context and fetch context into dedicated helper functions (e.g., `createQueryFnContext`), improving code readability. This change:
- Moves type assertions and type handling higher up, reducing inline type noise and making type boundaries clearer.
- Replaces ad-hoc object construction with a function that does one thing (build query contexts).
No functional behavior is changed; this is a structural and type-safety improvement.
* refactor: narrow onSuccess/onError/onMutate/onSettled callback types to Promise<void> | void
Previously, the `onSuccess`, `onError`, `onMutate`, and `onSettled` callbacks were typed as `() => Promise<unknown> | unknown`. While `unknown` is technically valid, it implies that the return value might be used, assigned, or further processed. However, throughout the codebase, these callbacks are invoked solely for their side effects, and their return values are always ignored. Narrowing the type to `Promise<void> | void` makes this intent explicit, clarifies that any return value will be discarded, and prevents misleading type signatures that suggest otherwise.
This commit narrows their types to `() => Promise<void> | void`, which more accurately reflects their intended use.
* docs: update callback return types to Promise<void> | void in mutation-related documentation
This commit refines the documentation for mutation-related callbacks (`onSuccess`, `onError`, `onSettled`, and `onMutate`), changing their return types from `Promise<unknown> | unknown` to `Promise<void> | void`.
* feat(core): StaleTime.Static
* fix: consider StaleTime.Static for invalidation
previously, it didn't matter if we included `stale: true` or not in the filters for refetch, because marking things with query.invalidate() would set them all to stale anyway.
Now, queries with StaleTime.Static will be marked as `invalidated`, but still shouldn't be refetched. The `isStale` filter logic accounts for that, so we have to include it in the filters
* fix: isStale order check
check for observers first because they contain the source of truth calculated with `isStaleByTime`, and it also takes enabled into account
* fix: isStaleByTime logic
we have to check for undefined data first, because queries without data are really always stale; then, the next check must be against StaleTime.Static, because queries that are marked as invalidated are still not stale, even if they are static.
* tests
* fix: type issue in react
* fix: make sure invalidation _always_ only refetches stale queries
this ensures we never refetch Static queries
* fix: never refetch static queries
* fix: make sure we don't refetchOn... for StaleTime.Static even when 'always' is set
* docs: StaleTime.Static
* ref: switch to 'static' string
* docs
* chore: update to eslint-plugin-react-hooks v6
and remove the separate compiler linter plugin, as those rules are now integrated
* chore: disable compiler-linter in tests
* chore: bypass lint violations
* chore: Disable '@eslint-react/no-context-provider'
to be react 18 compatible
Moved the NonUndefinedGuard type definition to the query-core package for better reusability across various query options files in angular, react, solid, svelte, and vue packages. Removed duplicate definitions from individual files.
* test(query-core): add test case for query
* test(query-core): use hashQueryKeyByOptions
* add test case about "should call initialData function when it is a function"
* reflect feedback
---------
Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
* test(hydration): implement missing hydration tests
* Do not hydrate older promises
* Do not infinite loop on hydrating failed promises
* Hydrate already resolved promises immediately
* fix(hydration): fix promise hydration bugs
* Include dehydratedAt and ignore hydrating old promises
* This is an approximation of dataUpdatedAt, but is not perfect
* Refactor checks for if we should hydrate promises or not to fix infinite loop bug on failed queries
* Hydrate already resolved promises synchronously
* call query.fetch even if sync data was available
* use noop function in tryResolveSync .catch
---------
Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
* feat(query-core): made "shouldThrowError" function
* refactor(react-query): apply "shouldThrowError" function in react-query package
* refactor(angular-query-experimental): apply "shouldThrowError" function
* refactor(solid-query): apply "shouldThrowError" function
* refactor(vue-query): apply "shouldThrowError" function
* test: add shouldThrowError function test
This is useful for example in thin app shell applications where feature teams provide their dependencies on lazy loaded route providers while still allowing to easily share a QueryClient.
This way the main bundle for the app shell does not include TanStack Query.