Skip to content

Commit 8981be4

Browse files
author
johannesmartikkala
committed
fix/correctly-infer-optional-query-parameters
1 parent ef984cf commit 8981be4

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/types.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export type OpenapiPaths<Paths> = {
1515

1616
// https://github.com/ajaishankar/openapi-typescript-fetch/issues/71#issuecomment-2847360598
1717
type NonNever<T> = [T] extends [never | undefined] ? unknown : T
18+
type IsAny<T> = 0 extends (1 & T) ? true : false;
1819

1920
export type OpArgType<OP> = OP extends {
2021
parameters?: {
@@ -119,15 +120,21 @@ export type FetchErrorType<F> = F extends TypedFetch<infer OP>
119120
? OpErrorType<OP>
120121
: never
121122

122-
type _CreateFetch<OP, Q = never> = [Q] extends [never]
123-
? () => TypedFetch<OP>
124-
: (query: Q) => TypedFetch<OP>
125-
126123
export type CreateFetch<M, OP> = M extends 'post' | 'put' | 'patch' | 'delete'
127-
? OP extends { parameters: { query: infer Q } }
128-
? _CreateFetch<OP, { [K in keyof Q]: true | 1 }>
129-
: _CreateFetch<OP>
130-
: _CreateFetch<OP>
124+
? OP extends {
125+
parameters: {
126+
query?: infer Q;
127+
};
128+
}
129+
? IsAny<Q> extends true
130+
? () => TypedFetch<OP>
131+
: [keyof Q] extends [never]
132+
? () => TypedFetch<OP>
133+
: {} extends Q
134+
? (query?: { [K in keyof Q]: true | 1 }) => TypedFetch<OP>
135+
: (query: { [K in keyof Q]: true | 1 }) => TypedFetch<OP>
136+
: () => TypedFetch<OP>
137+
: () => TypedFetch<OP>;
131138

132139
export type Middleware = (
133140
url: string,

0 commit comments

Comments
 (0)