문제

Possible Duplicate:
open-ended function arguments with TypeScript

Is there any acceptable type signature for variadic functions? Example:

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

console.log(sum(1, 2, 3, 4, 5));

gives me compilation error:

foo.ts(9,12): Supplied parameters do not match any signature of call target
도움이 되었습니까?

해결책

In TypeScript you can use "..." to achive the above pattern:

function sum (...numbers: number[]) {
  var sum = 0;
  for (var i = 0; i <  numbers.length; i++) {
    sum += numbers[i];
  }
  return sum;
};

This should take care of your error.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top