Skip to content
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

fix(types): infer the narrowest type with queryOptions #5805

Merged
merged 1 commit into from
Jul 31, 2023
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
13 changes: 13 additions & 0 deletions packages/react-query/src/__tests__/useQuery.types.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ describe('initialData', () => {
})
})

it('it should be possible to define a different TData than TQueryFnData using select with queryOptions spread into useQuery', () => {
doNotExecute(() => {
const options = queryOptions({
queryKey: ['key'],
queryFn: () => Promise.resolve(1),
})
useQuery({
...options,
select: (data) => data > 1,
})
})
})

it('TData should always be defined when initialData is provided as a function which ALWAYS returns the data', () => {
doNotExecute(() => {
const { data } = useQuery({
Expand Down
20 changes: 14 additions & 6 deletions packages/react-query/src/queryOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,26 @@ export function queryOptions<
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
): UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
TOptions extends UndefinedInitialDataOptions<
TQueryFnData,
TError,
TData,
TQueryKey
> = UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
>(options: TOptions): TOptions

export function queryOptions<
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
): DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
TOptions extends DefinedInitialDataOptions<
TQueryFnData,
TError,
TData,
TQueryKey
> = DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
>(options: TOptions): TOptions

export function queryOptions(options: unknown) {
return options
Expand Down
20 changes: 14 additions & 6 deletions packages/solid-query/src/createQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,26 @@ export function queryOptions<
TError = unknown,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
): UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
TOptions extends UndefinedInitialDataOptions<
TQueryFnData,
TError,
TData,
TQueryKey
> = UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
>(options: TOptions): TOptions

export function queryOptions<
TQueryFnData = unknown,
TError = unknown,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
): DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
TOptions extends DefinedInitialDataOptions<
TQueryFnData,
TError,
TData,
TQueryKey
> = DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
>(options: TOptions): TOptions

export function queryOptions(options: unknown) {
return options
Expand Down