Search code examples
javascriptthisgoogle-closure-compiler

How to define type of "this" in function with Closure Compiler


My goal with the Closure Compiler for my project is to achieve 100% typedness, but I'm struggling with a simple jQuery pattern that looks like this :

$Controls.on('change input', /** function(this:Element) */ function () {
    $(this).removeClass('is-invalid').next('.invalid-feedback').remove();
});

With or without the comment /** function(this:Element) */, I'm getting the following error:

js/quote.js:61: WARNING - could not determine the type of this expression
        $(this).removeClass('is-invalid').next('.invalid-feedback').remove();
          ^^^^

I thought I found that answer from Github Wiki titled 'Types in the Closure Type System' in the "Function this Type" section, but that doesn't seem to be doing what I thought it'd do.

How do I specify the type of this for Closure Compiler?


Solution

  • Thank to @RandyCasburn for his comment

    /** function(this:Element) */ should be changed to ** @this {Element} */ to essentially cast the type of the context of the function to Element

    I presume /** function(this:Element) */ should be used rather to define the type of an argument to a function with an element context