172

IMO, one of the main concerns of the TypeScript language is to support the existing vanilla JavaScript code. This is the impression I had at first glance. Take a look at the following JavaScript function which is perfectly valid:

Note: I am not saying that I like this approach. I am just saying this is a valid JavaScript code.

function sum(numbers) { 

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

    return agregatedNumber;
}

So, we consume this function with any number of arguments:

console.log(sum(1, 5, 10, 15, 20));

However, when I try this out with TypeScript Playground, it gives compile time errors.

I am assuming that this is a bug. Let's assume that we don't have the compatibility issues. Then, is there any way to write this type of functions with open-ended arguments? Such as params feature in C#?

2
  • 2
    Just curious, why do you even have a named numbers parameter? You're not doing anything with it. Aug 30, 2013 at 17:49
  • 2
    @JustinMorgan having at least one parameter lets Intellisense at least hint that there should be some arguments.
    – drzaus
    Jan 25, 2015 at 19:44

3 Answers 3

323

The TypeScript way of doing this is to place the ellipsis operator (...) before the name of the argument. The above would be written as,

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

This will then type check correctly with

console.log(sum(1, 5, 10, 15, 20));
3
  • Great! thanks. This is definitely the way to go. So, if someone has an existing code as in my question, it will break. Right?
    – tugberk
    Oct 2, 2012 at 19:59
  • 1
    Yes. It complains about the call but you can declare sum as accepting multiple parameters of any type by changing the signature to sum(...) instead an it will quiet the error. Please feel free to submit this as a bug on CodePlex.
    – chuckj
    Oct 2, 2012 at 20:01
  • 1
    Was looking for a rest parameter example. Ty.
    – A. M.
    Oct 3, 2012 at 16:45
11

In addition to @chuckj answer: You can also use an arrow function expression in TypeScript (is kind of a lambda in Java / .NET)

function sum(...nums: number[]): number {
    return nums.reduce((a, b) => a + b, 0);
}
6

In Typescript it is the concept of Rest Parameter, it is the parameter which receives multiple values of similar type.If we target the typescript then we have to write the code ECMAScript 6 standard,then typescript transpiler converts it to its equivalent java script code(which is ECMAScript 5 standard).If we use typescript then we have to use three dot(...) preferx with the restparameter variable name, such as function sum(...numbers: number[]), then it would work.

Note: Rest Parameter must be last parameter in the parameter list.likewise function sum(name:string,age:number,...numbers: number[]).

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.