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

isolatedModules documentation still says that it bans non-modules #55785

Closed
DetachHead opened this issue Sep 19, 2023 · 3 comments · Fixed by microsoft/TypeScript-Website#2951
Assignees
Labels
Docs The issue relates to how you learn TypeScript

Comments

@DetachHead
Copy link
Contributor

DetachHead commented Sep 19, 2023

🔎 Search Terms

isolatedmodules 5.0

🕗 Version & Regression Information

  • This changed between versions 4.9.5 and 5.0.4

⏯ Playground Link

💻 Code

const foo = 1

🙁 Actual behavior

no error

🙂 Expected behavior

'input.tsx' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module.

Additional information about the issue

i noticed there's a new moduleDetection compiler option. is "moduleDetection": "force" supposed to replace this use case of isolatedModules? if so, i would expect the error to still appear unless the file is detected as a module

@MartinJohns
Copy link
Contributor

MartinJohns commented Sep 19, 2023

This is intentional. See #46295 and linked issues. Used search terms: isolatedModules in:title

@DetachHead
Copy link
Contributor Author

DetachHead commented Sep 19, 2023

in that case, the isolatedModules documentation needs to be updated, because it still says this:

If isolatedModules is set, all implementation files must be modules (which means it has some form of import/export). An error occurs if any file isn’t a module

i always only used isolatedModules as a way to ban global scripts in my projects, i think many other people did the same, it's mentioned as a solution for this problem in many stackoverflow threads, eg:

https://stackoverflow.com/a/48360856
https://stackoverflow.com/a/56577324

so the updated documentation should probably also mention "moduleDetection": "force" as an alternative for this use case. seems like a pretty significant change that should have at least been mentioned in the 5.0 announcement post

@DetachHead DetachHead changed the title isolatedModules no longer works as of typescript 5.0.4 isolatedModules documentation still says that it bans non-modules Sep 19, 2023
@fatcerberus
Copy link

fatcerberus commented Sep 19, 2023

moduleDetection: "force" will cause the file to be detected as a module, for what it’s worth. It’s not really a direct replacement for banning global scripts per se, as it just prevents “this is a global script” from being detected at all - which IMO is better, since that’s how real module systems work anyway (i.e. script type is determined at load time and is not an intrinsic property of a JS file).

In other words: If you’re using isolatedModules today, you should keep using it in addition to moduleDetection: "force".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs The issue relates to how you learn TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants