google / google/closure-compiler

Properties from Object.assign(module.exports) escape type-checking

Open
#2,929 3 comments 0 reactions 0 assignees View on GitHub
bug P3
Dominant language
JavaScript
Stars
7.7k
Forks
1.2k
Avg merge
2d 12h
Merged PRs (30d)
6

Description

I'm compiling the following with `--process_common_js_modules`:
```
// a.js
const c = require("./c");
console.log(c.foo);

// c.js
module.exports = {};
Object.assign(module.exports, {
foo: 5,
});
```

and get an error:
```
a.js:11: WARNING - Property foo never defined on module$experimental$users$cyriln$js$c.default
console.log(c.foo);
^^^
```

even though the compiled output is functional:
```
// some $jscomp polyfills omitted
var module$experimental$users$cyriln$js$c$default = {};
Object.assign(module$experimental$users$cyriln$js$c$default, {foo:5});
console.log(module$experimental$users$cyriln$js$c$default.foo);
```

However, if the `Object.assign` is applied to a regular variable, typing works fine:
```
var c = {};
Object.assign(c, {
foo: 5,
});

module.exports = c;
```
and generates exactly same code.

Something is not right about processing `module.exports`.

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.