#Is it possible to use 'arguments' from function in typescript?

5 messages · Page 1 of 1 (latest)

violet sorrel
#

Is it possible to use arguments from Function in TS or i have to use rest operator?
Because when im trying to do this, im getting error:Expected 0 arguments, but got 4.ts(2554)

JS Example

function sum() {
  let result = 0;
  for (let i = 0; i < arguments.length; i++) {
    result += arguments[i];
  }
  return result;
}

console.log(sum(1, 2, 3, 4));
// Expected Output: 10
random tendon
#

Normally, you'd use function sum(...args: readonly number[]): number, I'm not even sure if there's a way to tell the compiler what types are inside arguments

#

arguments has absolutely no advantage... it's not even an array

violet sorrel
random tendon
#

If the goal is that the "generated" JS doesn't have ...args, then you can use an overload