BUG: (edge case) Typescript 4.5+ moduleResolution GlobalScope Introduce var global inside ESM Context edge case #47237
Labels
Committed
The team has roadmapped this issue
ES Next
New featurers for ECMAScript (a.k.a. ESNext)
feature-request
A request for a new feature
Fix Available
A PR has been opened for this issue
Milestone
Bug Report
global scope If your file hasn't any import or export line, this file would be executed in global scope that all declaration in it are visible outside this file.
This is a hard edge case as there is really no way to tell if that file will run in CJS or ESM Context When there is no additional package.json or if it is a .ts and not a .cts or mts
🔎 Search Terms
Typescript 4.5+ moduleResolution GlobalScope Introduce var global inside ESM Context
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
.mts and .ts files introduce none existing globals when the target Environment is "ECMAScript Module Context"
// xx.ts or xx.mts
// other.ts
That introduces now the following edge case TypeScript Assumes that .ts files without import or require get executed in the globalThis Context and with CJS that is correct! But Running the Same code in Module Context will not Introduce a Global Var on the globalThis Context.
🙂 Expected behavior
at last for .mts it should not introduce a new global. looking into package,json does not make clear if the target will be esm or cjs for the .ts extension it only shows the current development settings before compilation.
maybe the tsconfig target setting can be used i did not explore that fully but my first experiments show that a single target is well and should be keept while i see the need to define a full set of targets to process the input files right.
so the dev environment needs the be target environment aware while working with development files.
This is possible with the current target setting but many people develop for diffrent targets
and the most best would be if we could get errors in the language server based on all existing tsconfig files and the target property
Related Issues
The text was updated successfully, but these errors were encountered: