简体   繁体   English

我是否需要在 javascript/typescript 中为匿名函数指定返回类型?

[英]Do I need to specify a return type for an anonymous function in javascript / typescript?

I have the following function:我有以下功能:

    $('td:eq(' + iColumn + ') input', oSettings.oApi._fnGetTrNodes(oSettings))
        .each(function () {
            aData.push(this.value);
         });

In typescript I am getting a message saying:在打字稿中,我收到一条消息:

Error   3   Function declared a non-void return type, but has no return expression  

Why am I getting this message?为什么我会收到这条消息? I can resolve the message by saying "return true".我可以通过说“return true”来解决该消息。 Should I always specify a return type for this?我应该始终为此指定返回类型吗?

The signature of .each() in the jquery.d.ts file on the typescript repository is: .each()存储库中jquery.d.ts文件中.each()的签名是:

each(func: (index: any, elem: Element) => JQuery);

The jQuery documentation says that jQuery 文档说

We can stop the loop from within the callback function by returning false.我们可以通过返回 false 从回调函数中停止循环。

which implies that this jquery.d.ts is wrong.这意味着这个jquery.d.ts是错误的。

If you grab an updated version from Boris Yankov's repository , it will become:如果您从Boris Yankov 的存储库中获取更新版本,它将变为:

each(func: (index: any, elem: Element) => any);

This form will allow you to return anything, or nothing.此表格将允许您返回任何东西,或什么都不返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM