From 9941552bf6eedf86512d3b2c40f1dafd8ee10294 Mon Sep 17 00:00:00 2001 From: main Date: Mon, 16 Jun 2025 12:56:54 +0200 Subject: [PATCH] feat: introduce HttpMethod type and update RequestInit method typing Added a new HttpMethod type to define valid HTTP methods and updated the method property in RequestInit to use this type. Updated tests to reflect these changes. --- test/types/fetch.test-d.ts | 13 +++++++++++-- types/fetch.d.ts | 4 +++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/test/types/fetch.test-d.ts b/test/types/fetch.test-d.ts index aaae5429fb5..2acf28f171d 100644 --- a/test/types/fetch.test-d.ts +++ b/test/types/fetch.test-d.ts @@ -21,7 +21,8 @@ import { ResponseInit, ResponseType, ReferrerPolicy, - Dispatcher + Dispatcher, + HttpMethod } from '../..' const requestInit: RequestInit = {} @@ -33,6 +34,12 @@ const requestInit3: RequestInit = {} // Test assignment. See https://github.com/whatwg/fetch/issues/1445 requestInit3.credentials = 'include' const requestInit4: RequestInit = { body: null } +const requestInit5: RequestInit = { + method: 'GET' +} +const requestInit6: RequestInit = { + method: 'POST' +} declare const request: Request declare const headers: Headers @@ -44,7 +51,7 @@ expectType(requestInit.credentials) expectType(requestInit.headers) expectType(requestInit.integrity) expectType(requestInit.keepalive) -expectType(requestInit.method) +expectType(requestInit.method) expectType(requestInit.mode) expectType(requestInit.redirect) expectType(requestInit.referrer) @@ -55,6 +62,8 @@ expectType(requestInit.window) expectType(requestInit2.dispatcher) expectType(requestInit4.body) +expectType(requestInit5.method) +expectType(requestInit6.method) expectType(responseInit.status) expectType(responseInit.statusText) diff --git a/types/fetch.d.ts b/types/fetch.d.ts index 2cf502900d1..9e3b61b91bd 100644 --- a/types/fetch.d.ts +++ b/types/fetch.d.ts @@ -119,6 +119,8 @@ type RequestDestination = | 'worker' | 'xslt' +export type HttpMethod = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'PATCH' | (string & {}) + export interface RequestInit { body?: BodyInit | null cache?: RequestCache @@ -128,7 +130,7 @@ export interface RequestInit { headers?: HeadersInit integrity?: string keepalive?: boolean - method?: string + method?: HttpMethod; mode?: RequestMode redirect?: RequestRedirect referrer?: string