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

171
Vistas
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 Respuestas
Responde la pregunta

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 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