Skip to content

fix(types): useSuspenseQuery should type-narrow the Error field #9105

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

Merged
merged 6 commits into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/query-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import type { QueryFilters, QueryTypeFilter, SkipToken } from './utils'
import type { QueryCache } from './queryCache'
import type { MutationCache } from './mutationCache'

export type DistributiveOmit<
TObject,
TKey extends keyof TObject,
> = TObject extends any ? Omit<TObject, TKey> : never

export type OmitKeyof<
TObject,
TKey extends TStrictly extends 'safely'
Expand Down
11 changes: 11 additions & 0 deletions packages/react-query/src/__tests__/useSuspenseQuery.test-d.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,15 @@ describe('useSuspenseQuery', () => {
// @ts-expect-error TS2339
query.isPlaceholderData
})

it('should type-narrow the error field', () => {
const query = useSuspenseQuery({
queryKey: ['key'],
queryFn: () => Promise.resolve(5),
})

if (query.status === 'error') {
expectTypeOf(query.error).toEqualTypeOf<Error>()
}
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ describe('useSuspenseQuery', () => {

it('should render the correct amount of times in Suspense mode when gcTime is set to 0', async () => {
const key = queryKey()
let state: UseSuspenseQueryResult<number> | null = null
let state: UseSuspenseQueryResult<number, Error | null> | null = null

let count = 0
let renders = 0
Expand Down
3 changes: 2 additions & 1 deletion packages/react-query/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
DefaultError,
DefinedInfiniteQueryObserverResult,
DefinedQueryObserverResult,
DistributiveOmit,
InfiniteQueryObserverOptions,
InfiniteQueryObserverResult,
MutateFunction,
Expand Down Expand Up @@ -155,7 +156,7 @@ export type UseQueryResult<
export type UseSuspenseQueryResult<
TData = unknown,
TError = DefaultError,
> = OmitKeyof<
> = DistributiveOmit<
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@manudeli I think your OmitKeyof helper doesn’t work over union types (just like the build-in Omit also doesn’t work with it).

Because DefinedQueryObserverResult is a union of:

| QueryObserverRefetchErrorResult<TData, TError>
| QueryObserverSuccessResult<TData, TError>

it “doesn’t work” in a sense that type narrowing to error doesn’t happen anymore because it’s not distributive.

I’ve added the standard DistributiveOmit helper and things work fine, but if you can improve your helper type to not fail the added test case, we can switch back.

DefinedQueryObserverResult<TData, TError>,
'isPlaceholderData' | 'promise'
>
Expand Down
4 changes: 0 additions & 4 deletions packages/vue-query/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ export type DeepUnwrapRef<T> = T extends UnwrapLeaf
}
: UnwrapRef<T>

export type DistributiveOmit<T, TKeyOfAny extends keyof any> = T extends any
? Omit<T, TKeyOfAny>
: never

export interface DefaultOptions<TError = DefaultError> {
queries?: OmitKeyof<QueryObserverOptions<unknown, TError>, 'queryKey'> & {
/**
Expand Down
3 changes: 2 additions & 1 deletion packages/vue-query/src/useMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ import { useQueryClient } from './useQueryClient'
import type { ToRefs } from 'vue-demi'
import type {
DefaultError,
DistributiveOmit,
MutateFunction,
MutateOptions,
MutationObserverOptions,
MutationObserverResult,
} from '@tanstack/query-core'
import type { DistributiveOmit, MaybeRefDeep } from './types'
import type { MaybeRefDeep } from './types'
import type { QueryClient } from './queryClient'

type MutationResult<TData, TError, TVariables, TContext> = DistributiveOmit<
Expand Down