Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!