google / google/closure-compiler

Sometimes forgets types when importing via properties when using NODE resolution

Open
#3,803 5 comments 0 reactions 0 assignees View on GitHub
triage-done
Dominant language
JavaScript
Stars
7.7k
Forks
1.2k
Avg merge
2d 12h
Merged PRs (30d)
6

Description

When importing types using NODE resolution (i.e., via `require(…)`), Closure Compiler in some cases recognises the resulting types and in other cases does not. Consider:

types.js:
```JS
/** @constructor */
function Public() {}

/** @constructor */
Public.SubType = function() {};

/** @constructor */
function Private() {}

exports.Public = Public;
exports.testOnly = {Private};
```

test.js:
```JS
const {Public, testOnly} = require('./types.js');
const {Private} = testOnly;

let /** !Public */ pub; // OK
let /** !Public.SubType */ sub; // OK
let /** !Private */ private1; // WARNING: Unknown type Private
let /** !testonly.Private */ private2; // WARNING: Unknown type testOnly.Private
```

Output (v20210406):
```
$ google-closure-compiler -O=ADVANCED_OPTIMIZATIONS --module_resolution=NODE --process_common_js_modules types.js test.js --entry_point=test.js
test.js:6:9: WARNING - [JSC_UNRECOGNIZED_TYPE_ERROR] Bad type annotation. Unknown type Private
6| let /** !Private */ private1; // WARNING: Unknown type Private
^

test.js:7:9: WARNING - [JSC_UNRECOGNIZED_TYPE_ERROR] Bad type annotation. Unknown type testonly.Private
7| let /** !testonly.Private */ private2; // WARNING: Unknown type testOnly.Private
^

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

It's not really clear to me why it recognises `Public.SubType` as a type, but not `Private`. I'm not sure if this is a bug report or a feature request, but it is certainly one or the other as the present behaviour is unhelpful (and in a slightly insidious way: I've just spent several hours tracking down a bug caused by a typo that I was very surprised hadn't been caught by Closure Compiler—but it turned out it had no idea what type was being returned by calls to the `Private` constructor in my test harness because it had completely failed to infer a type here, and manually adding a type declaration provoked the unexpected warnings shown above).

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.