带有可选参数的 TypeScript lambda 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12763971/
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
TypeScript lambda functions with optional parameters
提问by nxn
I am having problems when defining a lambda function that accepts an optional parameter. The strange part is that if I use the full "function" syntax the anonymous function works, but the lambda shorthand/arrow syntax produces errors such as the following:
我在定义接受可选参数的 lambda 函数时遇到问题。奇怪的是,如果我使用完整的“函数”语法,匿名函数可以工作,但 lambda 速记/箭头语法会产生如下错误:
- The name 'a' does not exist in the current scope
- Supplied parameters do not match any signature of call target
- Expected ')'
- 当前作用域中不存在名称“a”
- 提供的参数与调用目标的任何签名都不匹配
- 预期的 ')'
Example:
例子:
(function (a, b?) => { console.log(a, b); })("a"); // OK
((a, b?) => { console.log(a, b); })("a", "b"); // Errors
((a, b) => { console.log(a, b); })("a", "b"); // OK
回答by Murat Sutunc
This is a bug in the compiler and is getting fixed right now [v0.8]. Lambdas currently give error messages with optional and rest parameters. Please use the long function syntax if this is a blocking issue.
这是编译器中的一个错误,现在正在修复 [v0.8]。Lambda 目前提供带有可选参数和其余参数的错误消息。如果这是一个阻塞问题,请使用长函数语法。
回答by Ryan Cavanaugh
There's currently a bug with optional parameter annotation in fat arrow lambda expressions.
当前在粗箭头 lambda 表达式中存在一个带有可选参数注释的错误。

