Skip to content

Commit f76d21b

Browse files
authoredJun 20, 2024··
fix(types): infer types correctly when method is omitted (#2551)
1 parent 8b4a408 commit f76d21b

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed
 

‎src/types/fetch/fetch.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,13 @@ export interface $Fetch<
7878
>(
7979
request: R,
8080
opts?: O
81-
): Promise<TypedInternalResponse<R, T, ExtractedRouteMethod<R, O>>>;
81+
): Promise<
82+
TypedInternalResponse<
83+
R,
84+
T,
85+
NitroFetchOptions<R> extends O ? "get" : ExtractedRouteMethod<R, O>
86+
>
87+
>;
8288
raw<
8389
T = DefaultT,
8490
R extends NitroFetchRequest = DefaultR,
@@ -87,7 +93,13 @@ export interface $Fetch<
8793
request: R,
8894
opts?: O
8995
): Promise<
90-
FetchResponse<TypedInternalResponse<R, T, ExtractedRouteMethod<R, O>>>
96+
FetchResponse<
97+
TypedInternalResponse<
98+
R,
99+
T,
100+
NitroFetchOptions<R> extends O ? "get" : ExtractedRouteMethod<R, O>
101+
>
102+
>
91103
>;
92104
create<T = DefaultT, R extends NitroFetchRequest = DefaultR>(
93105
defaults: FetchOptions

‎test/fixture/types.ts

+7
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,13 @@ describe("API routes", () => {
183183
});
184184

185185
it("generates the correct type depending on the method used", () => {
186+
expectTypeOf($fetch("/api/methods")).toEqualTypeOf<Promise<"Index get">>();
187+
expectTypeOf($fetch("/api/methods", {})).toEqualTypeOf<
188+
Promise<"Index get">
189+
>();
190+
expectTypeOf($fetch("/api/methods", { query: {} })).toEqualTypeOf<
191+
Promise<"Index get">
192+
>();
186193
expectTypeOf($fetch("/api/methods", { method: "get" })).toEqualTypeOf<
187194
Promise<"Index get">
188195
>();

0 commit comments

Comments
 (0)
Please sign in to comment.