google / google/closure-compiler

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

Open
#4,197 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
7.7k
Forks
1.2k
Avg merge
2d 12h
Merged PRs (30d)
6

Description

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) {
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.