@@ -156,21 +156,27 @@ export type MaybeOptionalInit<Params extends Record<HttpMethod, {}>, Location ex
156
156
? FetchOptions < FilterKeys < Params , Location > > | undefined
157
157
: FetchOptions < FilterKeys < Params , Location > > ;
158
158
159
+ // The final init param to accept.
160
+ // - Determines if the param is optional or not.
161
+ // - Performs arbitrary [key: string] addition.
162
+ // Note: the addition It MUST happen after all the inference happens (otherwise TS can’t infer if init is required or not).
163
+ type InitParam < Init > = HasRequiredKeys < Init > extends never
164
+ ? [ ( Init & { [ key : string ] : unknown } ) ?]
165
+ : [ Init & { [ key : string ] : unknown } ] ;
166
+
159
167
export type ClientMethod <
160
168
Paths extends Record < string , Record < HttpMethod , { } > > ,
161
169
Method extends HttpMethod ,
162
170
Media extends MediaType ,
163
171
> = < Path extends PathsWithMethod < Paths , Method > , Init extends MaybeOptionalInit < Paths [ Path ] , Method > > (
164
172
url : Path ,
165
- ...init : HasRequiredKeys < Init > extends never
166
- ? [ ( Init & { [ key : string ] : unknown } ) ?] // note: the arbitrary [key: string]: addition MUST happen here after all the inference happens (otherwise TS can’t infer if it’s required or not)
167
- : [ Init & { [ key : string ] : unknown } ]
173
+ ...init : InitParam < Init >
168
174
) => Promise < FetchResponse < Paths [ Path ] [ Method ] , Init , Media > > ;
169
175
170
176
export type ClientForPath < PathInfo extends Record < HttpMethod , { } > , Media extends MediaType > = {
171
- [ Method in keyof PathInfo as Uppercase < string & Method > ] : <
172
- Init extends MaybeOptionalInit < PathInfo , Method > ,
173
- > ( ) => Promise < FetchResponse < PathInfo [ Method ] , Init , Media > > ;
177
+ [ Method in keyof PathInfo as Uppercase < string & Method > ] : < Init extends MaybeOptionalInit < PathInfo , Method > > (
178
+ ... init : InitParam < Init >
179
+ ) => Promise < FetchResponse < PathInfo [ Method ] , Init , Media > > ;
174
180
} ;
175
181
176
182
export type Client < Paths extends Record < string , Record < HttpMethod , { } > > , Media extends MediaType = MediaType > = {
0 commit comments