Skip to content

Compiler outputs bad code when an (ES module) exported @typedef is assigned a value #3239

@shicks

Description

@shicks

The following code works correctly uncompiled:

// input0
/** @typedef {number} */
export const Bits = {};
Bits.of = () => 42;
Bits.of();
// input1
import {Bits} from './input0';
Bits.of();

But it compiles to

/** @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;
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):

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.modules and for single scripts. It's just ES modules that get it wrong.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ES6ModulesP3internal-issue-createdAn internal Google issue has been created to track this GitHub issuetriage-doneHas been reviewed by someone on triage rotation.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions