closure-compiler
closure-compiler copied to clipboard
Default function parameters typed as undefined when enclosed in an internal function
I think this looks like a bug in the compiler, see the minimal test case below.
The function parameter has a default, so it may never be "undefined" and the compiler honours this.
What I didn't expect is that enclosed in a function it somehow it picks up the possibility to be "undefined".
It happens for a range of types; function, string etc. The case where I noticed was a callback function provided to a constructor.
If I'm mistaken and this is not a bug but actually some edge case of the language then I'd also be open to hear of that. Many thanks.
/**
* @param {string} v
*/
function outer(v = 'fallback') {
inner(v); // <-- string
function local() {
inner(v); // <-- string|undefined ???
}
}
/**
* @param {string} v
*/
function inner(v) {
console.log(v);
}
outer();
$ java -jar closure-compiler-v20230502.jar -O ADVANCED test.js
test.js:9:14: WARNING - [JSC_TYPE_MISMATCH] actual parameter 1 of inner does not match formal parameter
found : (string|undefined)
required: string
9| inner(v);
^
0 error(s), 1 warning(s), 100.0% typed
(function(a="fallback"){console.log(a)})();