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

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

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