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

80
Vistas
conditionally call a function with its params

Having several functions, I would like do not call them if there is no need (logic in needToCalculate), used by a generic function calculateIfNeeded

// helper functions
function sum(a: number, b: number) { return a + b; }  // complex real logic (CRL)
function bum(a: number, code: string) { return (code=="B") ? a*a : a+a; } // CRL
function zum(code: string) { return (code=="Z") ? 5 : 8; }                // CRL

function needToCalculate(code: string) {return false} // CRL

Question: what type should have the param func of the method calculateIfNeeded bellow?

// calculator function
function calculateIfNeeded(code: string, func: ???, ...args: any[]){
    return needToCalculate(code) ? func(args): NaN
}

// main function
let a = 4, b = 9

let res = calculateIfNeeded("A", sum, a, b);  console.log(res);    
res = calculateIfNeeded("B", bum, a, "B");    console.log(res);    
res = calculateIfNeeded("C", zum, "Z");       console.log(res);

Playground

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

since it looks like you can return anything from the calculation (assuming you're not limited by any restrictions), the type of func will be

func: (...args: any[]) => any

That means you expected to receive in func any number of arguments with any type, and expect to return from func any type.

EDIT (inspired by @Dima Parzhitsky comment):

You can so something like this:

function calculateIfNeeded<F extends any[]>(code: string, func: (...args: F)=>number, ...args: F) : number{
    return needToCalculate(code) ? func(...args): NaN
}

Define F to be some extension of any arguments (any[]).

Then the func receives F and return any, and the args themselves are F as well.

Then you just call func(...args)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Extending upon my comment, you will have to add a parameter type, constraint it to the type of the function you want to use as func, and infer the type of args from that:

/**
 * This represents not just _any function_, but one that can be
 * given as the second parameter (`func`) of `calculateIfNeeded`.
 * The call signature must be added explicitly, because apparently
 * TypeScript cannot know, that a union of callables is also callable
 */
type Func = (typeof sum | typeof bum | typeof zum) & ((...args: never[]) => number);

function calculateIfNeeded<F extends Func>(code: string, func: F, ...args: Parameters<F>): number {
    return needToCalculate(code) ? func(...args): NaN;
}

Try it.

Note, that I had to change func(args) to func(...args), because args is an array, but neither of sum, bum, or zum accepts an array as an argument.

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