type Fn = (val?: number) => number | Fn;
const add = (val: number = 0): number | Fn => {
let sum = 0;
const curried = (b: number) => {
sum = sum + b
return curried;
};
curried.valueOf = () => sum;
curried.toString = () => sum;
return curried(val) as number | Fn;
};
console.log(add()) // => 0
console.log(add(1)(2)(3)) // => 6