Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

220
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda