Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

111
Vistas
Generic fetch wrapper

I am trying to write the wrapper under window.fetch.

type MyResponse<T> = {
    response: Response;
    data: T;
}

type MyResponseType = 'JSON' | 'EMPTY';

async function makeRequest<T, R extends MyResponseType>(url: string, responseType: R): Promise<MyResponse<R extends 'JSON' ? T : undefined>> {
    const response = await fetch(url);

    if (responseType === 'JSON') {
        const data = await response.json() as T;

        return { response, data } // Type 'T' is not assignable to type 'R extends "JSON" ? T : undefined'
    } else if (responseType === 'EMPTY') {
        return { response, data: undefined } // Type 'undefined' is not assignable to type 'R extends "JSON" ? T : undefined'
    } else {
        return { response, data: undefined } // Type 'undefined' is not assignable to type 'R extends "JSON" ? T : undefined'
    }
}

/*
Returns Promise<MyResponse<string>>, correct
*/
function fetchData() {
    return makeRequest<string, 'JSON'>('https://google.com', 'JSON')
}

/*
Returns Promise<MyResponse<undefined>>, correct
*/
function fetchNoData() {
    return makeRequest<void, 'EMPTY'>('https://google.com', 'EMPTY')
}

How to fix it?

Maybe I don't really need generic R or responseType param, but I would like to infer T if I pass anything that is not void | undefined, and I would like to infer undefined if I pass void | undefined.

Please don't give advice like "use axios", "use any (unknown)". It is a small piece of code from the project where I am not working single so I am not able to rewrite it, just to strongly type it.

A small refactor is up to you.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda