Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

79
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda