JSDoc inline return type with anonymous functions

1,538 views
Skip to first unread message

Hochhaus, Andy

unread,
Dec 5, 2016, 1:44:27 AM12/5/16
to closure-comp...@googlegroups.com
Hello,

How do you write a JSDoc inline return type with an anonymous function? Neither of the following seem to work?

goog.module('example');

exports.foo = /** boolean */ function() { return true; };
exports.bar = function /** boolean */ () { return true; };

-Andy

Erik Neumann

unread,
Dec 5, 2016, 2:17:18 AM12/5/16
to closure-comp...@googlegroups.com
Try this:

exports.foo = /** @type function(): boolean */ (function() { return true; });

The @type might be optional so this might also work:

exports.foo = /** function(): boolean */ (function() { return true; });

The parens around the value (in this case the function) are required for any type cast.

See the section "Type Expressions" near the end of this page:


or this:


The latter page has a section about "Type Casts".

--ErikN



--

---
You received this message because you are subscribed to the Google Groups "Closure Compiler Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to closure-compiler-discuss+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/closure-compiler-discuss/CAAhqAFp1T87%3DPTz3tQPzgLx0sFG5p61f_TA6bV1T%2Bx8Ek5hD9A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

John Munro

unread,
Dec 5, 2016, 11:29:10 AM12/5/16
to Closure Compiler Discuss
exports.foo = /** @return {boolean} */ function() { return true; };

Hochhaus, Andy

unread,
Dec 5, 2016, 1:19:22 PM12/5/16
to closure-comp...@googlegroups.com
Thanks.

Type casting the function seems minorly excessive to document the return type.

Using '@return' works but seems overly verbose given that function params can be annotated inline without using '@param' or '@type'.

According to the docs, named functions can document their return type as follows:

function /** number */ foo(x) { return x - 1; }

Does any corresponding syntax exist for anonymous functions (i.e. without requiring explicitly stating '@return')?

-Andy

Tyler Breisacher

unread,
Dec 5, 2016, 1:20:59 PM12/5/16
to closure-compiler
I don't think so. One approach would be to give it a short name such as perhaps "_" :

function /** number */ _(x) { return x - 1; }

--

---
You received this message because you are subscribed to the Google Groups "Closure Compiler Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to closure-compiler-discuss+unsub...@googlegroups.com.

Hochhaus, Andy

unread,
Dec 5, 2016, 1:28:20 PM12/5/16
to closure-comp...@googlegroups.com
On Mon, Dec 5, 2016 at 10:20 AM, 'Tyler Breisacher' via Closure
Compiler Discuss <closure-comp...@googlegroups.com> wrote:
> I don't think so. One approach would be to give it a short name such as perhaps "_" :

Sounds like the best work around. Thanks.
Reply all
Reply to author
Forward
0 new messages