diff --git a/.changeset/hot-tips-prove.md b/.changeset/hot-tips-prove.md new file mode 100644 index 0000000..0f5b6f1 --- /dev/null +++ b/.changeset/hot-tips-prove.md @@ -0,0 +1,5 @@ +--- +'@lukemorales/query-key-factory': patch +--- + +Loosen types for `TypedUseQueryOptions` and allow inference of dynamic query options generation diff --git a/src/utility-types.ts b/src/utility-types.ts index be5451a..87cbe28 100644 --- a/src/utility-types.ts +++ b/src/utility-types.ts @@ -1,10 +1,9 @@ -import type { UseQueryOptions } from '@tanstack/react-query'; +import type { QueryFunction, UseQueryOptions } from '@tanstack/react-query'; import type { QueryKeyStore } from './create-query-key-store'; import type { AnyQueryFactoryOutputCallback, AnyQueryKeyFactoryResult, - QueryOptionsStruct, StaticFactoryOutput, } from './create-query-keys.types'; import type { @@ -56,7 +55,19 @@ export type inferQueryKeyStore> = { [P in keyof Store]: inferQueryKeys; }; +type LooseQueryOptionsStruct = { queryKey: AnyMutableOrReadonlyArray; queryFn: QueryFunction }; + +type LooseQueryOptionsStructGenerator = (...args: any[]) => LooseQueryOptionsStruct; + export type TypedUseQueryOptions< - Options extends QueryOptionsStruct, - Data = Awaited>, -> = UseQueryOptions>, unknown, Data, Options['queryKey']>; + Options extends LooseQueryOptionsStruct | LooseQueryOptionsStructGenerator, + Data = Options extends LooseQueryOptionsStructGenerator + ? Awaited['queryFn']>> + : Options extends LooseQueryOptionsStruct + ? Awaited> + : never, +> = Options extends LooseQueryOptionsStructGenerator + ? UseQueryOptions['queryFn']>>, unknown, Data, ReturnType['queryKey']> + : Options extends LooseQueryOptionsStruct + ? UseQueryOptions>, unknown, Data, Options['queryKey']> + : never;