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

169
Visualizações
JavaScript/Typescript chainable function without class

I am trying to correctly code my chainable function in Typescript. Below is the code

  const sum = (val: number | undefined) => {
    let internal = Number(val);
    if (val) {
      return function sumup(_val: number | undefined) {
        if (_val && internal !== undefined) {
          internal += _val;
          return sumup;
        }
        return internal;
      };
    }
    return internal;
  };

  console.log('sum:', sum(1)(2)(3)());

The function is working to the point that I get the correct result. However, it is yielding a typescript error: This expression is not callable. Not all constituents of type 'number | ((_val: number | undefined) => number | ...)' are callable. Type 'number' has no call signatures.

How would I correctly code such a sum function without using class or this?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can achieve this by describing precisely the functions behaviors with function overloads:

function sum(): number
function sum(val: number): typeof sum
function sum(val?: number): number | typeof sum {
  let internal = Number(val);
  if (val) {
    function sumup(): number
    function sumup(_val: number): typeof sumup
    function sumup(_val?: number): number | typeof sumup {
      if (_val && internal !== undefined) {
        internal += _val;
        return sumup;
      }
      return internal;
    };
    return sumup;
  }
  return internal;
};

console.log('sum:', sum(1)(2)(3)());

TypeScript playground

over 4 years ago · Santiago Trujillo 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