@@ -15,6 +15,9 @@ import {
1515import type { ClientMethod , FetchResponse , MaybeOptionalInit , Client as FetchClient } from "openapi-fetch" ;
1616import type { HttpMethod , MediaType , PathsWithMethod , RequiredKeysOf } from "openapi-typescript-helpers" ;
1717
18+ // Helper type to dynamically infer the type from the `select` property
19+ type InferSelectReturnType < TData , TSelect > = TSelect extends ( data : TData ) => infer R ? R : TData ;
20+
1821type InitWithUnknowns < Init > = Init & { [ key : string ] : unknown } ;
1922
2023export type QueryKey <
@@ -29,7 +32,12 @@ export type QueryOptionsFunction<Paths extends Record<string, Record<HttpMethod,
2932 Init extends MaybeOptionalInit < Paths [ Path ] , Method > ,
3033 Response extends Required < FetchResponse < Paths [ Path ] [ Method ] , Init , Media > > , // note: Required is used to avoid repeating NonNullable in UseQuery types
3134 Options extends Omit <
32- UseQueryOptions < Response [ "data" ] , Response [ "error" ] , Response [ "data" ] , QueryKey < Paths , Method , Path > > ,
35+ UseQueryOptions <
36+ Response [ "data" ] ,
37+ Response [ "error" ] ,
38+ InferSelectReturnType < Response [ "data" ] , Options [ "select" ] > ,
39+ QueryKey < Paths , Method , Path >
40+ > ,
3341 "queryKey" | "queryFn"
3442 > ,
3543> (
@@ -40,11 +48,21 @@ export type QueryOptionsFunction<Paths extends Record<string, Record<HttpMethod,
4048 : [ InitWithUnknowns < Init > , Options ?]
4149) => NoInfer <
4250 Omit <
43- UseQueryOptions < Response [ "data" ] , Response [ "error" ] , Response [ "data" ] , QueryKey < Paths , Method , Path > > ,
51+ UseQueryOptions <
52+ Response [ "data" ] ,
53+ Response [ "error" ] ,
54+ InferSelectReturnType < Response [ "data" ] , Options [ "select" ] > ,
55+ QueryKey < Paths , Method , Path >
56+ > ,
4457 "queryFn"
4558 > & {
4659 queryFn : Exclude <
47- UseQueryOptions < Response [ "data" ] , Response [ "error" ] , Response [ "data" ] , QueryKey < Paths , Method , Path > > [ "queryFn" ] ,
60+ UseQueryOptions <
61+ Response [ "data" ] ,
62+ Response [ "error" ] ,
63+ InferSelectReturnType < Response [ "data" ] , Options [ "select" ] > ,
64+ QueryKey < Paths , Method , Path >
65+ > [ "queryFn" ] ,
4866 SkipToken | undefined
4967 > ;
5068 }
@@ -56,7 +74,12 @@ export type UseQueryMethod<Paths extends Record<string, Record<HttpMethod, {}>>,
5674 Init extends MaybeOptionalInit < Paths [ Path ] , Method > ,
5775 Response extends Required < FetchResponse < Paths [ Path ] [ Method ] , Init , Media > > , // note: Required is used to avoid repeating NonNullable in UseQuery types
5876 Options extends Omit <
59- UseQueryOptions < Response [ "data" ] , Response [ "error" ] , Response [ "data" ] , QueryKey < Paths , Method , Path > > ,
77+ UseQueryOptions <
78+ Response [ "data" ] ,
79+ Response [ "error" ] ,
80+ InferSelectReturnType < Response [ "data" ] , Options [ "select" ] > ,
81+ QueryKey < Paths , Method , Path >
82+ > ,
6083 "queryKey" | "queryFn"
6184 > ,
6285> (
@@ -65,15 +88,20 @@ export type UseQueryMethod<Paths extends Record<string, Record<HttpMethod, {}>>,
6588 ...[ init , options , queryClient ] : RequiredKeysOf < Init > extends never
6689 ? [ InitWithUnknowns < Init > ?, Options ?, QueryClient ?]
6790 : [ InitWithUnknowns < Init > , Options ?, QueryClient ?]
68- ) => UseQueryResult < Response [ "data" ] , Response [ "error" ] > ;
91+ ) => UseQueryResult < InferSelectReturnType < Response [ "data" ] , Options [ "select" ] > , Response [ "error" ] > ;
6992
7093export type UseSuspenseQueryMethod < Paths extends Record < string , Record < HttpMethod , { } > > , Media extends MediaType > = <
7194 Method extends HttpMethod ,
7295 Path extends PathsWithMethod < Paths , Method > ,
7396 Init extends MaybeOptionalInit < Paths [ Path ] , Method > ,
7497 Response extends Required < FetchResponse < Paths [ Path ] [ Method ] , Init , Media > > , // note: Required is used to avoid repeating NonNullable in UseQuery types
7598 Options extends Omit <
76- UseSuspenseQueryOptions < Response [ "data" ] , Response [ "error" ] , Response [ "data" ] , QueryKey < Paths , Method , Path > > ,
99+ UseSuspenseQueryOptions <
100+ Response [ "data" ] ,
101+ Response [ "error" ] ,
102+ InferSelectReturnType < Response [ "data" ] , Options [ "select" ] > ,
103+ QueryKey < Paths , Method , Path >
104+ > ,
77105 "queryKey" | "queryFn"
78106 > ,
79107> (
@@ -82,7 +110,7 @@ export type UseSuspenseQueryMethod<Paths extends Record<string, Record<HttpMetho
82110 ...[ init , options , queryClient ] : RequiredKeysOf < Init > extends never
83111 ? [ InitWithUnknowns < Init > ?, Options ?, QueryClient ?]
84112 : [ InitWithUnknowns < Init > , Options ?, QueryClient ?]
85- ) => UseSuspenseQueryResult < Response [ "data" ] , Response [ "error" ] > ;
113+ ) => UseSuspenseQueryResult < InferSelectReturnType < Response [ "data" ] , Options [ "select" ] > , Response [ "error" ] > ;
86114
87115export type UseMutationMethod < Paths extends Record < string , Record < HttpMethod , { } > > , Media extends MediaType > = <
88116 Method extends HttpMethod ,
0 commit comments