feat(fetch): introduce HttpMethod type and update RequestInit method typing#4282
feat(fetch): introduce HttpMethod type and update RequestInit method typing#4282plvo wants to merge 1 commit into
HttpMethod type and update RequestInit method typing#4282Conversation
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.
Isn't this a breaking change? It won't be possible to merge of declarations of the following |
|
@tsctx You're right about the issue. I've found this is a fundamental TS limitation after testing:
But it works like this import "undici"
import type { HttpMethod } from "undici";
declare module "undici" {
interface RequestInit {
method?: HttpMethod;
}
} |
| | 'worker' | ||
| | 'xslt' | ||
|
|
||
| export type HttpMethod = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'PATCH' | (string & {}) |
There was a problem hiding this comment.
Maybe do it like we do it in fastify?
https://github.com/fastify/fastify/blob/main/types/utils.d.ts
Also I realize now, you could force uppercase strings for HttpMethod.
There was a problem hiding this comment.
@Uzlopak good idea to use Autocomplete, the code is cleaner that way :
export type HTTPMethods =
| 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'PATCH'
export type HTTPMethod = Autocomplete<HTTPMethods | Lowercase<HTTPMethods>>
export interface RequestInit {
method?: HTTPMethod
// ...
}however, there's still the problem of module augmentation with method? : string
There was a problem hiding this comment.
Can you be more specific, please? What do you mean withmodule augmentation?
There was a problem hiding this comment.
I was referring to this message when I was talking about the module augmentation
This relates to...
This PR enhances the developer experience by adding proper TypeScript typing for HTTP methods and missing RequestInit properties to match the standard Fetch API specification. https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Methods
Added a new
HttpMethodtype to define valid HTTP methods and updated the method property inRequestInitto use this type. Updated tests to reflect these changes.Rationale
Changes
Enhanced TypeScript Definitions
HttpMethodtype: Union of standard HTTP methods + extensible string type using(string & {})patternRequestInit.method: Now typed asHttpMethodinstead of genericstringTest Coverage
HttpMethodtyping works correctlyFeatures
(string & {})patternBug Fixes
Breaking Changes and Deprecations
None - This is a purely additive change that maintains full backward compatibility. All existing code will continue to work without modification.
Status