@@ -15,6 +15,9 @@ import {
15
15
import type { ClientMethod , FetchResponse , MaybeOptionalInit , Client as FetchClient } from "openapi-fetch" ;
16
16
import type { HttpMethod , MediaType , PathsWithMethod , RequiredKeysOf } from "openapi-typescript-helpers" ;
17
17
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
+
18
21
type InitWithUnknowns < Init > = Init & { [ key : string ] : unknown } ;
19
22
20
23
export type QueryKey <
@@ -29,7 +32,12 @@ export type QueryOptionsFunction<Paths extends Record<string, Record<HttpMethod,
29
32
Init extends MaybeOptionalInit < Paths [ Path ] , Method > ,
30
33
Response extends Required < FetchResponse < Paths [ Path ] [ Method ] , Init , Media > > , // note: Required is used to avoid repeating NonNullable in UseQuery types
31
34
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
+ > ,
33
41
"queryKey" | "queryFn"
34
42
> ,
35
43
> (
@@ -40,11 +48,21 @@ export type QueryOptionsFunction<Paths extends Record<string, Record<HttpMethod,
40
48
: [ InitWithUnknowns < Init > , Options ?]
41
49
) => NoInfer <
42
50
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
+ > ,
44
57
"queryFn"
45
58
> & {
46
59
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" ] ,
48
66
SkipToken | undefined
49
67
> ;
50
68
}
@@ -56,7 +74,12 @@ export type UseQueryMethod<Paths extends Record<string, Record<HttpMethod, {}>>,
56
74
Init extends MaybeOptionalInit < Paths [ Path ] , Method > ,
57
75
Response extends Required < FetchResponse < Paths [ Path ] [ Method ] , Init , Media > > , // note: Required is used to avoid repeating NonNullable in UseQuery types
58
76
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
+ > ,
60
83
"queryKey" | "queryFn"
61
84
> ,
62
85
> (
@@ -65,15 +88,20 @@ export type UseQueryMethod<Paths extends Record<string, Record<HttpMethod, {}>>,
65
88
...[ init , options , queryClient ] : RequiredKeysOf < Init > extends never
66
89
? [ InitWithUnknowns < Init > ?, Options ?, QueryClient ?]
67
90
: [ InitWithUnknowns < Init > , Options ?, QueryClient ?]
68
- ) => UseQueryResult < Response [ "data" ] , Response [ "error" ] > ;
91
+ ) => UseQueryResult < InferSelectReturnType < Response [ "data" ] , Options [ "select" ] > , Response [ "error" ] > ;
69
92
70
93
export type UseSuspenseQueryMethod < Paths extends Record < string , Record < HttpMethod , { } > > , Media extends MediaType > = <
71
94
Method extends HttpMethod ,
72
95
Path extends PathsWithMethod < Paths , Method > ,
73
96
Init extends MaybeOptionalInit < Paths [ Path ] , Method > ,
74
97
Response extends Required < FetchResponse < Paths [ Path ] [ Method ] , Init , Media > > , // note: Required is used to avoid repeating NonNullable in UseQuery types
75
98
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
+ > ,
77
105
"queryKey" | "queryFn"
78
106
> ,
79
107
> (
@@ -82,7 +110,7 @@ export type UseSuspenseQueryMethod<Paths extends Record<string, Record<HttpMetho
82
110
...[ init , options , queryClient ] : RequiredKeysOf < Init > extends never
83
111
? [ InitWithUnknowns < Init > ?, Options ?, QueryClient ?]
84
112
: [ InitWithUnknowns < Init > , Options ?, QueryClient ?]
85
- ) => UseSuspenseQueryResult < Response [ "data" ] , Response [ "error" ] > ;
113
+ ) => UseSuspenseQueryResult < InferSelectReturnType < Response [ "data" ] , Options [ "select" ] > , Response [ "error" ] > ;
86
114
87
115
export type UseMutationMethod < Paths extends Record < string , Record < HttpMethod , { } > > , Media extends MediaType > = <
88
116
Method extends HttpMethod ,
0 commit comments