Skip to content

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

Closed

Description

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 :).

Activity

  1. brendankenny commented on Sep 14, 2018

    @brendankenny
    ContributorAuthor

    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.

  2. 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[/+] on Sep 14, 2018
  3. 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[/+] on Sep 14, 2018
  4. sandersn commented on Sep 14, 2018

    @sandersn
    Member

    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.

  5. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions