-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Allow isolatedModules
with global scripts, and better match build tool capabilities
#46295
Comments
The litmus test for what I think the broader suggestion of allowing scripts to be compiled under |
isolatedModules
with global scripts, and better match build tool capabilities
Thanks @andrewbranch, I agree and have reworded the issue like you suggested. I had originally been thinking specifically about the challenges with the Babel namespace limitations, but it really is a larger problem of getting a massive codebase to be compatible with other build tools for emit. Having And then it's just a question of whether namespaced global scripts can be maintainable when Babel is used for emit. It seems like with the right compiler help (preventing unqualified namespace references), maybe they could be. |
Suggestion
The
--isolatedModules
flag is typically used to ensure compatibility with build tools that don't have access to type information.One restriction currently is that global (non-module) scripts are not allowed at all when this flag is enabled. This restriction does not match up with the capabilities of tools like the Babel
@babel/plugin-transform-typescript
plugin. The Babel plugin is able to process global scripts and even has impartial namespace support.It is difficult to migrate an existing codebase to use the Babel transform plugin if it has a lot of existing global script /
namespace
code.⭐ Suggestion
Minimally, it would be nice if the
isolatedModules
option did not raise errors for global scripts. (Enforcing "all modules" seems like it could be separate option.)Ideally, there would be a way to configure the TypeScript compiler to do type checking that matches the capabilities of build tools like the Babel plugin.
For example, the
isolatedModules
option could behave as follows:📃 Motivating Example
The example from the Babel documentation, modified slightly:
As the Babel documentation describes, TypeScript knows to emit
Company.V
inside the second namespace because of its understanding of types declared globally across files, but Babel has no idea and just emitsV
.It is not uncommon for existing legacy codebases to have a lot of code like this. A company could have put most of their code inside a namespace and be frequently accessing types without qualifying.
💻 Use Cases
isolatedModules
option.Alternatives
It might be possible to write something that can identify problem cases like unqualified namespace references. (A one-off script, maybe even a linter rule using type info.) That could help with getting a codebase into a state where the code emits safely through Babel. It doesn't seem that this would be convenient to use while maintaining code going forward.
🔍 Search Terms
babel transform namespaces
✅ Viability Checklist
My suggestion meets these guidelines:
isolatedModules
to raise fewer errors, so possibly a noticeable change to existing behavior.The text was updated successfully, but these errors were encountered: