-
Notifications
You must be signed in to change notification settings - Fork 12.8k
.cts
files are should not share the same top-level scope
#49207
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
Comments
I didn't realize that CTS files are meant to be authored using ESM syntax, and then TS will properly scope them. |
It sounds like the problem here is that the That disambiguation is useful for |
I was wrong - the disambiguation is more subtle. The disambiguation logic in // @target: esnext
// @module: nodenext
// @allowJs
// @checkJs
// filename: a.cjs
const same = 1;
// ^ error TS2451: Cannot redeclare block-scoped variable 'same'.
console.log(same);
// filename: b.cjs
const same = 1;
// ^ error TS2451: Cannot redeclare block-scoped variable 'same'.
console.log(same); A (bad) workaround is to add an inert line to each file to trigger this module mode detection, such as: // Inform TypeScript this file is not a script
("exports" in module) || (module.exports = {}); You have to go pretty far down the rabbit hole to figure this out, so I hope we can get auto-detection for this. |
I'm seeing the error in this file, which does export something 🤔 |
The rule appears to be:
|
Repro: // test.cts
const a = 1;
// test2.cts
const a = 1; It's still warning about redeclaring |
IIRC the " |
Tested with |
|
Just a small update: we discussed this a bit today - we'll update |
Is this supposed to work by default in TC 4.7.3? I'm still getting errors like this if I don't enable
This is my config: {
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"lib": ["esnext"],
"declaration": true,
"declarationMap": true,
"emitDeclarationOnly": true,
"declarationDir": "./dts",
"moduleResolution": "node",
"esModuleInterop": true,
"isolatedModules": true,
"skipLibCheck": true,
"resolveJsonModule": true,
"noImplicitThis": true
}
}
|
@nicolo-ribaudo does it work in nightly? Trying to figure out if the merge went badly or if it's just broken |
It also fails with |
Bug Report
NOTE: There are already multiple (open or closed) issues about this topic. However, this issue is specifically about the behavior in
.cts
files and not in.ts
files..ts
files disallow using top-level block-scoped variables with the same name because they would conflict when loaded with a<script>
tag..cts
files currently have the same restriction, however:For these reasons, I think that this restriction should be removed.
A real word example: I'm updating
typescript
to 4.7 in the Babel repository, and I hoped to be able to convert our remaining JavaScript files to TypeScript. They are still JS because we needed to use the.cjs
extension: https://github.com/babel/babel/tree/main/eslint/babel-eslint-parser/src. However, most of those files use the same variables names to import other files, so I'm unable to do so.🔎 Search Terms
cts top-level Cannot redeclare block-scoped variable
🕗 Version & Regression Information
.cts
has been introduced⏯ Playground Link
n/a (it requires multiple files)
💻 Code
file1.cts
:file2.cts
:🙁 Actual behavior
🙂 Expected behavior
No error
The text was updated successfully, but these errors were encountered: