Javascript 使用 TypeScript 的开放式函数参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12697275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:03:42  来源:igfitidea点击:

open-ended function arguments with TypeScript

javascripttypescript

提问by tugberk

IMO, one of the main concerns of the TypeScriptlanguage 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:

IMO,TypeScript语言的主要关注点之一是支持现有的 vanilla JavaScript 代码。这是我第一眼的印象。看看以下完全有效的 JavaScript 函数:

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

注意:我并不是说我喜欢这种方法。我只是说这是一个有效的 JavaScript 代码。

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.

但是,当我使用TypeScript Playground尝试此操作时,它会出现编译时错误。

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 paramsfeature in C#?

我假设这是一个错误。假设我们没有兼容性问题。那么,有没有办法用开放式参数编写这种类型的函数?例如paramsC# 中的功能?

回答by chuckj

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

执行此操作的 TypeScript 方法是将省略号运算符 ( ...) 放在参数名称之前。上面会写成,

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));

回答by Jeroen van Dijk-Jun

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

除了@chuckj 答案:您还可以arrow function expression在 TypeScript 中使用 an (类似于lambdaJava/.NET 中的 a)

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

回答by Sheo Dayal Singh

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 6standard,then typescript transpiler converts it to its equivalent java script code(which is ECMAScript 5standard).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.

在 Typescript 中它是Rest Parameter的概念,它是接收多个相似类型值的参数。如果我们针对 typescript 那么我们必须编写代码ECMAScript 6标准,然后 typescript 转换器将其转换为等效的 java 脚本代码(这是ECMAScript 5标准。如果我们使用 typescript,那么我们必须使用三个 dot(...) preferredx 和 restparameter 变量名称,例如 function sum(...numbers: number[]),那么它会起作用。

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

注意:Rest Parameter 必须是参数列表中的最后一个参数。同样的函数 sum(name:string,age:number,...numbers: number[])。