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

142
Views
How can I type a resulting function based on the return type of an argument function

I have two functions:

export const processCar = (params: IParams): Car => {
    // ...
}

export const processBoat = (params: IParams): Boat => {
    // ...
}

I have another function to which the first two functions will be passed as arguments:

const processOrNull = (func: any): any => {
    const newFunc = (params: IParams): any => {
        // Perform some check. If it fails, return null. If it passes...

        return func(params);
    }
    return newFunc;
}

export const processCarOrNull = processOrNull(processCar);  // (IParams) => Car | null;
export const processBoatOrNull = processOrNull(processBoat);    // (IParams) => Boat | null;

I want the result from processOrNull to be typed as either (IParams) => (Car | null) or (IParams) => (Boat | null), depending on whether the passed function returns a Car or a Boat.

Can I do this with generics and if so, how?

about 4 years ago · Santiago Gelvez
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!