google / google/closure-compiler
Compiler outputs bad code when an (ES module) exported @typedef is assigned a value
- Dominant language
- JavaScript
- Stars
- 7.7k
- Forks
- 1.2k
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 6
Description
The following code works correctly uncompiled:
```js
// input0
/** @typedef {number} */
export const Bits = {};
Bits.of = () => 42;
Bits.of();
```
```js
// input1
import {Bits} from './input0';
Bits.of();
```
But it compiles to
```js
/** @const @typedef {number} */ var Bits$$module$input0 = {};
/** @return {!Bits$$module$input0} */
Bits$$module$input0.of = function() { return 42; };
Bits$$module$input0.of();
/** @const */ var module$input0 = {};
/** @typedef {Bits$$module$input0} */ module$input0.Bits;
```
```js
module$input0.Bits.of();
/** @const */ var module$input1 = {};
```
Note that the child module calls `module$input0.Bits.of()` while the parent module calls `Bits$$module$input0.of()`. Moreover, the previous two lines (at the end of the parent) are (removing comments):
```js
var module$input0 = {};
module$input0.Bits;
module$input0.Bits.of();
```
It's very clear that this will be a runtime error, since the `module$input0.Bits` stub is not actually defined. This works correctly for `goog.module`s and for single scripts. It's just ES modules that get it wrong.
Contributor guide
Assessment
This issue has not been assessed yet.