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
?