简体   繁体   中英

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". 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(func: (index: any, elem: Element) => JQuery);

The jQuery documentation says that

We can stop the loop from within the callback function by returning false.

which implies that this jquery.d.ts is wrong.

If you grab an updated version from Boris Yankov's repository , it will become:

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

This form will allow you to return anything, or nothing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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