typedef a function?

83 views
Skip to first unread message

Fredrik

unread,
Sep 30, 2010, 5:33:12 AM9/30/10
to Closure Compiler Discuss
Hi,

When creating an extern file I've come across a case where I wan't to
"typedef" a namespaced
function to a shorter, global alias.
But I can't seem to find the correct annotation to achieve this (short
of duplicating the entire annotation again).

The example code below might seem to work at first glance, "foo" is
treated as a function, but it seems to silently drop type-checking for
any parameters.

Am I misunderstanding something, or should I file an issue?

(perhaps @inheritdoc could be modfied to be more generic?)

--------
Example:

----- extern.js -------
var NS = {};

/**
* @param {string} a
*/
NS.foo = function(a) {};

/**
* try to create a global alias for the namespaced function NS.foo
* @typedef {NS.foo}
*/
var foo;
//--------------------------


//------ code.js ----
NS.foo(123) // warning ok.
foo(123); // no warning!
//--------------

Regards
// Fredrik

André Moraes

unread,
Sep 30, 2010, 11:03:53 AM9/30/10
to closure-comp...@googlegroups.com
Maybe

var foo = NS.foo?

--
André Moraes
http://andredevchannel.blogspot.com/

Fredrik

unread,
Sep 30, 2010, 11:12:42 AM9/30/10
to Closure Compiler Discuss
Nope. Tried that, same error I mention above, it produces an extern
var but the type-checking is lost :(

John Lenz

unread,
Sep 30, 2010, 11:27:25 AM9/30/10
to closure-comp...@googlegroups.com
The compiler "NS.foo" isn't a named type, only @constructer, @enum, @interface, and @typedef introduce named types.

Usually, you would simply repeat the definition.  Alternately, you can create a typedef to be used in both places:

/** @typedef {function(string):void} */
var fntype;

var NS = {};

/** @type {fntype} */
NS.foo;

/** @type {fntype} */
var foo;

Nick Santos

unread,
Sep 30, 2010, 11:28:27 AM9/30/10
to closure-comp...@googlegroups.com
@typedef {NS.foo} doesn't really make sense in this context. It's a bit subtle, because @typedef accepts a type expression, and you're expecting it to be a reference expression.

if {NS.foo} was a reference expression, it would refer to the function assigned to NS.foo.

but because {NS.foo} is a expression, it refers to an *instance* of NS.foo (which is not possible, because NS.foo is not a constructor).

does that make sense?

in this particular case, the only real solution is to repeat the annotation i think.

Fredrik

unread,
Sep 30, 2010, 12:32:42 PM9/30/10
to Closure Compiler Discuss
Thanks, yes it makes more sense now!

Regards
// Fredrik
Reply all
Reply to author
Forward
0 new messages