Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.1rc (js) regression: writing to property of a global variable redefines entire variable type #27099

Closed
brendankenny opened this issue Sep 14, 2018 · 2 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@brendankenny
Copy link
Contributor

brendankenny commented Sep 14, 2018

TypeScript Versions: 3.1.0-rc.20180911, 3.1.0-dev.20180914

Not an issue in 3.1.0-dev.20180831 (bisected to #26908)

Search Terms:
does not exist global write module

Code

// test.js
const INPUT = process.argv;
process.env.ENV_VAR = 'val';

module.exports = INPUT;

Run tsc --noEmit --allowJs --checkJs with @types/node available (see below for a non-node example).

Expected behavior:
No error.

Actual behavior:
error TS2339: Property 'argv' does not exist on type 'typeof process'.

Not a problem in typescript code. When all the example lines are present, the definition and type definition of process now point to the process.env.ENV_VAR line instead of to @types/node.

To get the bug, either module.exports needs to be set or a file needs to be required or imported, so I assume it being a module is important. Writing to anything on process seems to trigger it, not just a sub-property (so e.g. process.env = {ENV_VAR: 'val'}; would also trigger the bug, though probably not a good idea in real code :).

@brendankenny
Copy link
Contributor Author

brendankenny commented Sep 14, 2018

bisecting leads to #26908.

It seems to occur for any globals, e.g.

// test.js
window.name = 'name';
window.console.log(5); // error TS2339: Property 'console' does not exist on type 'typeof window'.
module.exports = 'anything';

It looks like issue #27057 may be the same issue, but I haven't cloned and run the project given in the example to verify.

@brendankenny brendankenny changed the title 3.1rc (js) regression: writing to property of global process redefines entire 'process' type 3.1rc (js) regression: writing to property of global object redefines entire object type Sep 14, 2018
@brendankenny brendankenny changed the title 3.1rc (js) regression: writing to property of global object redefines entire object type 3.1rc (js) regression: writing to property of a global variable redefines entire variable type Sep 14, 2018
@ahejlsberg ahejlsberg added the Bug A bug in TypeScript label Sep 14, 2018
@ahejlsberg ahejlsberg added this to the TypeScript 3.1 milestone Sep 14, 2018
@sandersn
Copy link
Member

sandersn commented Sep 14, 2018

Repros on 3.0 with an expando initializer, like window.name = {}. The bug is more visible now that initializers aren't required to be expandos. The core of the problem is in iniitalizeTypeChecker:

if (!isExternalOrCommonJsModule(file)) {
  mergeSymbolTable(globals, file.locals!);
}

CommonJS modules should still merge with globals, because, in JS, the compiler allows undeclared identifiers to act as namespaces in exactly the way assignment to window works.

sandersn added a commit that referenced this issue Sep 14, 2018
Makes commonjs merge with globals when appropriate.
sandersn added a commit that referenced this issue Sep 16, 2018
* Fix cross-file merge of assignment decl valueDeclaration

Previously mergeSymbol in the checker always updated valueDeclaration if
target.valueDeclaration was an assignment declaration. The binder only
updates target.valueDeclaration if it is an assignment declaration and
source.valueDeclaration is *not* an assignment declaration. Now the
checker behaves the same way as the binder.

* Update baselines

* Add a fix for #27099

Makes commonjs merge with globals when appropriate.

* Add a separate jsGlobalAugmentations table

Instead of trying to filter these augmentations out of the normal symbol
table of commonjs modules.
@sandersn sandersn added the Fixed A PR has been merged for this issue label Sep 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants