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

225
Vistas
how to use generic this type in this case ? i don't know why

recently i learned typescript generic. and i used it in certain situations. but i couldn't.

When using two different interfaces, i wanted it.

for example

i wanna send server request.

function fetcher async(){
    return await axios.get(url).then(data=>data.result)
}

and server gives me responses but, they have two different types

// response A
interfase AInterface { 
 name: string;
 age: number;
 address: string
}

// response B
interfase BInterface {
 name: string;
 job : string;
 haveCar: boolean;
}

and set return type by using generic

function fetcher<T> async():<T>{
    return await axios.get(url).then(data=>data.result)
}
and use function

const result = fetcher<AInterface>()
                or fetcher<BInterface>()

result has type AInterface | BInterface

why does result have type AInterface | BInterface ??

i don't know

i want it has AInterface or BInterface . Not union

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You dont need to type fetcher

just use:

async function fetcher() {
    const { data } = await axios.get<AInterface>(url)
    return data
}

Or:

async function fetcher(): Promise<AInterface> {
    const { data } = await axios.get(url)
    return data
}
about 4 years ago · Juan Pablo Isaza Denunciar
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