Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions packages/query-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import type { QueryFilters, QueryTypeFilter, SkipToken } from './utils'
import type { QueryCache } from './queryCache'
import type { MutationCache } from './mutationCache'

export type DistributiveOmit<TObject, TKey> = TObject extends any
? Pick<TObject, Exclude<keyof 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>()
}
})
})
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
Loading