Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7823165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:09:01+00:00 2026-06-02T08:09:01+00:00

I’m messing around with the type checking in google’s closure compiler. The type system

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T08:09:05+00:00Added an answer on June 2, 2026 at 8:09 am

    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;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm making a simple page using Google Maps API 3. My first. One marker
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have a jquery bug and I've been looking for hours now, I can't
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.