Google closure: problem type checking options that should be features

I was messing with type checking in google close compiler. A type system seems useful if it may not be the most complex. I am pleased with most of the limitations, but it just seems a little strange.

I see problems with type annotations for functions passed as arguments. In particular, if the type of the function passed is not itself fixed. For example, I would 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 the following:

test.js:17: ERROR - left side of numeric comparison
found   : *
required: number
    filter([1,2,3], function (x) { return x > 1; });
                                          ^

So what happened? Can I indicate that a parameter should have a function with one argument, without specifying the type of this argument? Am I doing something wrong, or is it just a typing restriction?


, , :

filter([1,2,3], function (x) { return /** @type {number} */ (x) > 1; });

(), ( ?) . :

/**
* @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);

, . :

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);

.

+3
4

"*" () "?" (). . , , "x" "?" ( ) ( -), "*" ( ), :

/**
 * @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;
};
+4

, , . :

/** @return {undefined} */
function MyFunction() {}

, .

( , ):

filter([1,2,3], function (x) { return /** @type {number} */ (x) > 1; });
+3

- {!Function}, .

(*) ​​: 708

+2
+1

All Articles