microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.92k stars 12.47k forks source link

Crash when exporting `globalThis` in `declare global` block #33754

Closed falsandtru closed 5 years ago

falsandtru commented 5 years ago

TypeScript Version: 3.4.0-dev.201xxxxx

Search Terms:

Code

declare global {
  export { globalThis as global };
}

Expected behavior:

pass

Actual behavior:

$ node built/local/tsc --noEmit index.ts
...\TypeScript\built\local\tsc.js:94648
                throw e;
                ^

TypeError: Cannot read property '0' of undefined

Playground Link:

Related Issues:

shicks commented 5 years ago

FWIW, here's the full stack trace I'm seeing:

TypeError: Cannot read property '0' of undefined
    at checkExportSpecifier (/tmp/xx/node_modules/typescript/lib/tsc.js:52303:124)
    at Object.forEach (/tmp/xx/node_modules/typescript/lib/tsc.js:225:30)
    at checkExportDeclaration (/tmp/xx/node_modules/typescript/lib/tsc.js:52269:24)
    at checkSourceElementWorker (/tmp/xx/node_modules/typescript/lib/tsc.js:52569:28)
    at checkSourceElement (/tmp/xx/node_modules/typescript/lib/tsc.js:52408:17)
    at Object.forEach (/tmp/xx/node_modules/typescript/lib/tsc.js:225:30)
    at checkBlock (/tmp/xx/node_modules/typescript/lib/tsc.js:50207:20)
    at checkSourceElementWorker (/tmp/xx/node_modules/typescript/lib/tsc.js:52518:28)
    at checkSourceElement (/tmp/xx/node_modules/typescript/lib/tsc.js:52408:17)
    at checkModuleDeclaration (/tmp/xx/node_modules/typescript/lib/tsc.js:52064:17)
sandersn commented 5 years ago

@falsandtru can you explain why this should pass? The errors I get after fixing the crash look correct:

welove.ts:1:9 - error TS2669: Augmentations for the global scope can only be directly nested in external modules or ambient module declarations.

1 declare global {
          ~~~~~~

welove.ts:2:14 - error TS2661: Cannot export 'globalThis'. Only local declarations can be exported from a module.

2     export { globalThis as global }
               ~~~~~~~~~~

Found 2 errors.
sandersn commented 5 years ago

See also #33756

falsandtru commented 5 years ago

Looks like correct. I'll file bugs again if unfixed bugs exist.