Skip to content

Commit 2de4acc

Browse files
myabcclaude
andauthored
🤖 Merge PR DefinitelyTyped#74614 [hotwired__turbo] Fix fetchOptions.headers type by @myabc
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 1963960 commit 2de4acc

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

‎types/hotwired__turbo/hotwired__turbo-tests.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ StreamActions.log = function() {
9494
console.log(this.getAttribute("message"));
9595
};
9696

97+
document.addEventListener("turbo:before-fetch-request", function(event) {
98+
// $ExpectType FetchRequestHeaders
99+
const headers = event.detail.fetchOptions.headers;
100+
headers["Turbo-Referrer"] = window.location.href;
101+
// $ExpectType string | undefined
102+
headers.Accept;
103+
});
104+
97105
document.addEventListener("turbo:before-fetch-response", function(e) {
98106
let { fetchResponse } = e.detail;
99107
fetchResponse.header("foo");

‎types/hotwired__turbo/index.d.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,15 @@ export class StreamMessage {
7878
constructor(fragment: DocumentFragment);
7979
}
8080

81+
export interface FetchRequestHeaders {
82+
[header: string]: string | undefined;
83+
}
84+
8185
export class FetchRequest {
8286
body: FormData | URLSearchParams;
8387
enctype: "application/x-www-form-urlencoded" | "multipart/form-data" | "text/plain";
8488
fetchOptions: RequestInit;
85-
headers: Headers | { [k: string]: any };
89+
headers: FetchRequestHeaders;
8690
method: "get" | "post" | "put" | "patch" | "delete";
8791
params: URLSearchParams;
8892
target: HTMLFormElement | HTMLAnchorElement | FrameElement | null;
@@ -489,9 +493,9 @@ export type TurboFrameMissingEvent = CustomEvent<{
489493
}>;
490494

491495
export type TurboBeforeFetchRequestEvent = CustomEvent<{
492-
fetchOptions: RequestInit;
496+
fetchOptions: Omit<RequestInit, "headers"> & { headers: FetchRequestHeaders };
493497
url: URL;
494-
resume: (value: any) => void;
498+
resume: (value?: unknown) => void;
495499
}>;
496500

497501
export type TurboBeforeFetchResponseEvent = CustomEvent<{

0 commit comments

Comments
 (0)