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

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