closure-compiler
closure-compiler copied to clipboard
Type checking const "this.*" incorrectly checks outside of scope where names overlap
In this minimal test case the compiler seems to be (incorrectly) type checking against a variable declared outside the scope?
- the
@constis 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) {
}
}