Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default function parameters typed as undefined when enclosed in an internal function #4100

Open
hills opened this issue Jul 6, 2023 · 0 comments

Comments

@hills
Copy link

hills commented Jul 6, 2023

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)})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant