Skip to content

fix(RequestInit): restrict body type based on method to improve type safety #1953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion baselines/dom.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1953,7 +1953,7 @@ interface ReportingObserverOptions {

interface RequestInit {
/** A BodyInit object or null to set request's body. */
body?: BodyInit | null;
body?: 'GET' | 'HEAD' extends this['method'] ? never : BodyInit | null | null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work if method is a general string, e.g.:

function download(url: URL, method: string, body: any) {
  return fetch(url, { method, body });
}

Or method is lowercased or something weird like "gET" (this still works in browsers as the browser internally makes it uppercase). Actually not sure this would work even if method is GET/HEAD, can you add a unit test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, when I think about it more, it seems that this is a missing feature in TypeScript. TS needs to have a Template Literal Type for case-insensitive matching.

/** A string indicating how the request will interact with the browser's cache to set request's cache. */
cache?: RequestCache;
/** A string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL. Sets request's credentials. */
Expand Down
2 changes: 1 addition & 1 deletion baselines/serviceworker.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ interface ReportingObserverOptions {

interface RequestInit {
/** A BodyInit object or null to set request's body. */
body?: BodyInit | null;
body?: 'GET' | 'HEAD' extends this['method'] ? never : BodyInit | null | null;
/** A string indicating how the request will interact with the browser's cache to set request's cache. */
cache?: RequestCache;
/** A string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL. Sets request's credentials. */
Expand Down
2 changes: 1 addition & 1 deletion baselines/sharedworker.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ interface ReportingObserverOptions {

interface RequestInit {
/** A BodyInit object or null to set request's body. */
body?: BodyInit | null;
body?: 'GET' | 'HEAD' extends this['method'] ? never : BodyInit | null | null;
/** A string indicating how the request will interact with the browser's cache to set request's cache. */
cache?: RequestCache;
/** A string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL. Sets request's credentials. */
Expand Down
2 changes: 1 addition & 1 deletion baselines/ts5.5/dom.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1953,7 +1953,7 @@ interface ReportingObserverOptions {

interface RequestInit {
/** A BodyInit object or null to set request's body. */
body?: BodyInit | null;
body?: 'GET' | 'HEAD' extends this['method'] ? never : BodyInit | null | null;
/** A string indicating how the request will interact with the browser's cache to set request's cache. */
cache?: RequestCache;
/** A string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL. Sets request's credentials. */
Expand Down
2 changes: 1 addition & 1 deletion baselines/ts5.5/serviceworker.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ interface ReportingObserverOptions {

interface RequestInit {
/** A BodyInit object or null to set request's body. */
body?: BodyInit | null;
body?: 'GET' | 'HEAD' extends this['method'] ? never : BodyInit | null | null;
/** A string indicating how the request will interact with the browser's cache to set request's cache. */
cache?: RequestCache;
/** A string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL. Sets request's credentials. */
Expand Down
2 changes: 1 addition & 1 deletion baselines/ts5.5/sharedworker.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ interface ReportingObserverOptions {

interface RequestInit {
/** A BodyInit object or null to set request's body. */
body?: BodyInit | null;
body?: 'GET' | 'HEAD' extends this['method'] ? never : BodyInit | null | null;
/** A string indicating how the request will interact with the browser's cache to set request's cache. */
cache?: RequestCache;
/** A string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL. Sets request's credentials. */
Expand Down
2 changes: 1 addition & 1 deletion baselines/ts5.5/webworker.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ interface ReportingObserverOptions {

interface RequestInit {
/** A BodyInit object or null to set request's body. */
body?: BodyInit | null;
body?: 'GET' | 'HEAD' extends this['method'] ? never : BodyInit | null | null;
/** A string indicating how the request will interact with the browser's cache to set request's cache. */
cache?: RequestCache;
/** A string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL. Sets request's credentials. */
Expand Down
2 changes: 1 addition & 1 deletion baselines/webworker.generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ interface ReportingObserverOptions {

interface RequestInit {
/** A BodyInit object or null to set request's body. */
body?: BodyInit | null;
body?: 'GET' | 'HEAD' extends this['method'] ? never : BodyInit | null | null;
/** A string indicating how the request will interact with the browser's cache to set request's cache. */
cache?: RequestCache;
/** A string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL. Sets request's credentials. */
Expand Down
3 changes: 3 additions & 0 deletions inputfiles/overridingTypes.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -3924,6 +3924,9 @@
"member": {
"window": {
"overrideType": "null"
},
"body": {
"overrideType": "'GET' | 'HEAD' extends this['method'] ? never : BodyInit | null"
}
}
}
Expand Down
Loading