google / google/closure-compiler
Infer optional parameter with default value as not undefined in closure
- Dominant language
- JavaScript
- Stars
- 7.7k
- Forks
- 1.2k
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 6
Description
If an argument to a function typed as an array has the default value of `[]`, we know it will never be undefined. I know I can cast, just sayin'.
```js
class A {
ok(prop) {
console.log('this is ok: %s', prop)
}
}
/**
* @param {!Array} a
* @param {string} prop
*/
function hello(a, prop) {
a.forEach(b => b.ok(prop))
}
/**
* @param {!Array} [props]
* @param {!Array} [a]
*/
function test(props = [], a = []) {
props.forEach((prop) => {
hello(a, prop) // we know a is never undefined
})
}
test()
```
```sh
t/infer.js:21: WARNING - actual parameter 1 of hello does not match formal parameter
found : (Array|undefined)
required: Array
hello(a, prop)
^
0 error(s), 1 warning(s), 93.5% typed
```
Contributor guide
Assessment
This issue has not been assessed yet.