Google closure: trouble type checking parameters that should be functions-Collection of common programming errors
I’m messing around with the type checking in google’s closure compiler. The type system seems useful, if perhaps not the most sophisticated out there. I’m happy with most of the limitations, but this one just seems a bit weird.
I’m seeing problems giving type annotations for functions passed as arguments. In particular, if the type of the passed function is itself not fixed. So for example, I’d like to write code similar to this:
/**
* @param {Array} xs
* @param {function(*) : boolean} f
* @return {Array}
*/
var filter = function (xs, f) {
var i, result = [];
for (i = 0; i < xs.length; i += 1) {
if (f(xs[i])) {
result.push(v);
}
}
return result;
};
filter([1,2,3], function (x) { return x > 1; });
Passing “–js_error checkTypes” to the compiler, I get this:
test.js:17: ERROR - left side of numeric comparison
found : *
required: number
filter([1,2,3], function (x) { return x > 1; });
^
So, what’s wrong? Can I specify that a parameter ought to a function with one argument, without specifying the type of that argument? Am I doing something wrong, or is this just a limitation of the type checker?
Chad suggests annotating the anonymous function passed to filter to help the type-inference out a bit:
filter([1,2,3], function (x) { return /** @type {number} */ (x) > 1; });
That works ok for filter(), but it seems a little unsatisfying (why does the compiler need that annotation?), and doesn’t work for more complex cases. For example:
/**
* @param {Array|string} as
* @param {Array|string} bs
* @param {function(*, *): *} f
* @return {Array}
*/
var crossF = function (as, bs, f) {};
/**
* @param {Array|string} as
* @param {Array|string} bs
* @return {Array}
*/
var cross = function (as, bs) {};
var unitlist = crossF(['AB', 'CD'], ['12', '34'], cross);
It seems like the type of everything here should be apparent to the compiler. And in fact it complains directly about matching the type of the function parameter:
test.js:52: ERROR - actual parameter 3 of crossF does not match formal parameter
found : function ((Array|null|string), (Array|null|string)): (Array|null)
required: function (*, *): *
var unitlist = crossF(['ABC', 'DEF', 'GHI'], ['123', '456', '789'], cross);
Accepted answer below addresses this case.
-
Change the declaration of the filter from “” (everything) to “?” (unknown). The compiler only checks known types. So when the compiler tries to infer the function signature for the function expression at the call site, it resolves the parameter “x” to “?” (an unknown type) (which can be used as anything), instead of “” (every possible type) which often needs to be restricted before use:
/** * @param {Array} xs * @param {function(?) : boolean} f * @return {Array} */ var filter = function (xs, f) { var i, result = []; for (i = 0; i < xs.length; i += 1) { if (f(xs[i])) { result.push(v); } } return result; };
-
When there are no annotations on a function, the compiler assumes that it can take a variable number of arguments of any type and return any type. For this reason, many of the extern functions are annotated like this:
/** @return {undefined} */ function MyFunction() {}
This way they will properly type check.
For your case, the easiest solution is to type cast the argument to a number inside the function (note the extra parenthesis which are required):
filter([1,2,3], function (x) { return /** @type {number} */ (x) > 1; });
-
One common approach is to use the type annotation
{!Function}
, which accepts any function object.The issue with the ALL type (*) has been reported here: Issue 708
Originally posted 2013-11-09 19:01:45.