Skip to content

Instantly share code, notes, and snippets.

@ugultopu
Created March 31, 2023 14:23
Show Gist options
  • Select an option

  • Save ugultopu/e8cd1b44eef9b0c8192199fc7c2fb2db to your computer and use it in GitHub Desktop.

Select an option

Save ugultopu/e8cd1b44eef9b0c8192199fc7c2fb2db to your computer and use it in GitHub Desktop.

Revisions

  1. ugultopu created this gist Mar 31, 2023.
    17 changes: 17 additions & 0 deletions Generic fetch for TypeScript.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    If you wished that you had the ability of declaring the response body's types for your [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/fetch) calls beforehand (where you made the call), you can use the following `fetch` wrapper:

    ```typescript
    interface GenericResponse<T> extends Response {
    clone(): GenericResponse<T>;
    json(): Promise<T>;
    }

    export default async function genericFetch<T>(
    input: RequestInfo | URL,
    init?: RequestInit
    ) {
    return fetch(input, init) as Promise<GenericResponse<T>>;
    }
    ```

    Inspired by [ts-generic-fetch](https://www.npmjs.com/package/ts-generic-fetch).