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

163
Visualizações
How to correctly use generics in typescript?

I get stuck with generics . How can I correctly use generics in my case:

export interface Location {
  id: number;
  address: {
    houseNumber: string;
  };
}
export const getEuropeLocations = async (
  apiKey: string
) => {
  let response = await axios.get<Location>({ 
    method: "get",
    url: `${apiBaseUrl}europe/`,
    headers: {
      Authorization: `Bearer ${apiKey}`,
    },
  });
  return response.data;
};

have error with types : "{ "message": "Argument of type '{ method: string; url: string; headers: { Authorization: string; }; }' is not assignable to parameter of type 'string'.",

}"

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Axios has typings for this. It lets you specify a generic type for the axios functions so that result.data will have that generic type.

For example, you can do this:

import axios from "axios";

export const getEuropeLocations = async (
  apiKey: string
) => {
  let response = await axios.get<Location>(`${apiBaseUrl}europe/`, {
    headers: {
      Authorization: `Bearer ${apiKey}`,
    },
  });
  return response.data;
};

If you want to make the entire function generic, you can do this:

export const getEuropeLocations = async <T>(
  apiKey: string
) => {
  let response = await axios.get<T>(`${apiBaseUrl}europe/`, {
    headers: {
      Authorization: `Bearer ${apiKey}`,
    },
  });
  return response.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