Skip to content

Commit

Permalink
fix(types): infer types correctly when method is omitted (#2551)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Jun 20, 2024
1 parent 8b4a408 commit f76d21b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/types/fetch/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,13 @@ export interface $Fetch<
>(
request: R,
opts?: O
): Promise<TypedInternalResponse<R, T, ExtractedRouteMethod<R, O>>>;
): Promise<
TypedInternalResponse<
R,
T,
NitroFetchOptions<R> extends O ? "get" : ExtractedRouteMethod<R, O>
>
>;
raw<
T = DefaultT,
R extends NitroFetchRequest = DefaultR,
Expand All @@ -87,7 +93,13 @@ export interface $Fetch<
request: R,
opts?: O
): Promise<
FetchResponse<TypedInternalResponse<R, T, ExtractedRouteMethod<R, O>>>
FetchResponse<
TypedInternalResponse<
R,
T,
NitroFetchOptions<R> extends O ? "get" : ExtractedRouteMethod<R, O>
>
>
>;
create<T = DefaultT, R extends NitroFetchRequest = DefaultR>(
defaults: FetchOptions
Expand Down
7 changes: 7 additions & 0 deletions test/fixture/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,13 @@ describe("API routes", () => {
});

it("generates the correct type depending on the method used", () => {
expectTypeOf($fetch("/api/methods")).toEqualTypeOf<Promise<"Index get">>();
expectTypeOf($fetch("/api/methods", {})).toEqualTypeOf<
Promise<"Index get">
>();
expectTypeOf($fetch("/api/methods", { query: {} })).toEqualTypeOf<
Promise<"Index get">
>();
expectTypeOf($fetch("/api/methods", { method: "get" })).toEqualTypeOf<
Promise<"Index get">
>();
Expand Down

0 comments on commit f76d21b

Please sign in to comment.