You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
chore: improve type safety in getQueryData by using generics (#9218)
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.
0 commit comments