Skip to content

Instantly share code, notes, and snippets.

@ugultopu
Created March 31, 2023 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ugultopu/e8cd1b44eef9b0c8192199fc7c2fb2db to your computer and use it in GitHub Desktop.
Save ugultopu/e8cd1b44eef9b0c8192199fc7c2fb2db to your computer and use it in GitHub Desktop.

If you wished that you had the ability of declaring the response body's types for your fetch calls beforehand (where you made the call), you can use the following fetch wrapper:

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment