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?

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.