From f0bc02dd467cd2c21b6048687865cf4d0ec8dbce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Sat, 29 Jul 2023 16:09:58 +0200 Subject: [PATCH] fix(types): infer the narrowest type with `queryOptions` --- .../src/__tests__/useQuery.types.test.tsx | 13 ++++++++++++ packages/react-query/src/queryOptions.ts | 20 +++++++++++++------ packages/solid-query/src/createQuery.ts | 20 +++++++++++++------ 3 files changed, 41 insertions(+), 12 deletions(-) diff --git a/packages/react-query/src/__tests__/useQuery.types.test.tsx b/packages/react-query/src/__tests__/useQuery.types.test.tsx index 18eaf870a7..f285ad155f 100644 --- a/packages/react-query/src/__tests__/useQuery.types.test.tsx +++ b/packages/react-query/src/__tests__/useQuery.types.test.tsx @@ -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({ diff --git a/packages/react-query/src/queryOptions.ts b/packages/react-query/src/queryOptions.ts index aafe953036..323743ac8c 100644 --- a/packages/react-query/src/queryOptions.ts +++ b/packages/react-query/src/queryOptions.ts @@ -24,18 +24,26 @@ export function queryOptions< TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, ->( - options: UndefinedInitialDataOptions, -): UndefinedInitialDataOptions + TOptions extends UndefinedInitialDataOptions< + TQueryFnData, + TError, + TData, + TQueryKey + > = UndefinedInitialDataOptions, +>(options: TOptions): TOptions export function queryOptions< TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, ->( - options: DefinedInitialDataOptions, -): DefinedInitialDataOptions + TOptions extends DefinedInitialDataOptions< + TQueryFnData, + TError, + TData, + TQueryKey + > = DefinedInitialDataOptions, +>(options: TOptions): TOptions export function queryOptions(options: unknown) { return options diff --git a/packages/solid-query/src/createQuery.ts b/packages/solid-query/src/createQuery.ts index 6eb48eb1c0..6d9f6261e9 100644 --- a/packages/solid-query/src/createQuery.ts +++ b/packages/solid-query/src/createQuery.ts @@ -39,18 +39,26 @@ export function queryOptions< TError = unknown, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, ->( - options: UndefinedInitialDataOptions, -): UndefinedInitialDataOptions + TOptions extends UndefinedInitialDataOptions< + TQueryFnData, + TError, + TData, + TQueryKey + > = UndefinedInitialDataOptions, +>(options: TOptions): TOptions export function queryOptions< TQueryFnData = unknown, TError = unknown, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, ->( - options: DefinedInitialDataOptions, -): DefinedInitialDataOptions + TOptions extends DefinedInitialDataOptions< + TQueryFnData, + TError, + TData, + TQueryKey + > = DefinedInitialDataOptions, +>(options: TOptions): TOptions export function queryOptions(options: unknown) { return options