google / google/closure-compiler

"Null" coalescing type unable to be inferred

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

Description

http://en.wikipedia.org/wiki/Null_coalescing_operator#JavaScript

``` javascript
/** @type {!Window|!Document} */
var x;
/** @type {!Document} */
var y = x instanceof Window ? x.document : x;
/** @type {!Document} */
var z = (x instanceof Window && x.document || x);
```

with `--jscomp_error=checkTypes` generates:

```
test.js:6: ERROR - initializing variable
found : (Document|Window)
required: Document
var z = (x instanceof Window && x.document || x);
^

1 error(s), 0 warning(s), 100.0% typed
```

`y` is inferred correctly whereas `z` not.

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.