closure-compiler icon indicating copy to clipboard operation
closure-compiler copied to clipboard

Type checking const "this.*" incorrectly checks outside of scope where names overlap

Open hills opened this issue 1 year ago • 2 comments

In this minimal test case the compiler seems to be (incorrectly) type checking against a variable declared outside the scope?

  • the @const is necessary
  • both functions must be declared with the same name
  • the name "zzz" can be anything
  • global scope is not special; it can be all wrapped in a function with the same result
$ java -jar closure-compiler-v20240317.jar -O ADVANCED  test.js
test.js:14:4: WARNING - [JSC_TYPE_MISMATCH] assignment to property zzz of MyObject
found   : function(boolean): undefined
required: function(number): undefined
  14|     this.zzz = zzz;
          ^^^^^^^^^^^^^^

0 error(s), 1 warning(s), 100.0% typed
/**
 * @param {!number} w
 */

function zzz(w) {     // this function should be irrelevant
}

/**
 * @constructor
 */

function MyObject() {
    /** @const */
    this.zzz = zzz;   // fails the type check

    /**
     * @param {boolean} v
     */

    function zzz(v) {
    }
}

hills avatar Oct 24 '24 14:10 hills