diff --git a/types/react-native/globals.d.ts b/types/react-native/globals.d.ts index 92ae988ecb..2110ab7278 100644 --- a/types/react-native/globals.d.ts +++ b/types/react-native/globals.d.ts @@ -25,7 +25,11 @@ declare function fetchBundle(bundleId: number, callback: (error?: Error | null) // Fetch API // -declare function fetch(input: Request | string, init?: RequestInit): Promise; +declare interface GlobalFetch { + fetch(input: RequestInfo, init?: RequestInit): Promise; +} + +declare function fetch(input: RequestInfo, init?: RequestInit): Promise; interface Blob {} @@ -85,6 +89,8 @@ declare var Request: { new(input: Request | string, init?: RequestInit): Request; }; +declare type RequestInfo = Request | string; + declare interface ResponseInit { headers?: HeadersInit_; status?: number; diff --git a/types/react-native/test/globals.tsx b/types/react-native/test/globals.tsx new file mode 100644 index 0000000000..b5eae800b1 --- /dev/null +++ b/types/react-native/test/globals.tsx @@ -0,0 +1,26 @@ +const fetchCopy: GlobalFetch['fetch'] = fetch; + +const myHeaders = new Headers(); +myHeaders.append('Content-Type', 'image/jpeg'); + +const myInit: RequestInit = { + method: 'GET', + headers: myHeaders, + mode: 'cors', +}; + +const myRequest = new Request('flowers.jpg'); + +fetch(myRequest, myInit).then((response) => { + console.log(response.type); + console.log(response.url); + console.log(response.status); + console.log(response.ok); + console.log(response.statusText); + console.log(response.headers); + + return response.blob(); +}).then((blob) => { + const init = { status: 200, statusText: 'SuperSmashingGreat!' } + const myResponse = new Response(blob, init); +}) diff --git a/types/react-native/tsconfig.json b/types/react-native/tsconfig.json index 16c6bf9275..c577fb0d8b 100644 --- a/types/react-native/tsconfig.json +++ b/types/react-native/tsconfig.json @@ -20,9 +20,10 @@ "files": [ "index.d.ts", "test/index.tsx", + "test/globals.tsx", "test/animated.tsx", "test/init-example.tsx", "test/ART.tsx", "test/legacy-properties.tsx" ] -} \ No newline at end of file +}