2

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?

Igor Shmukler
  • 1,742
  • 3
  • 15
  • 48

1 Answers1

2

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

Guerric P
  • 30,447
  • 6
  • 48
  • 86