-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
"declare global" affects all compiled modules even when the source file is in "module" mode #22576
Comments
A global variable is accessible in every scope. You don't have to import global variables. If you want a variable to only be available when you import a module, use The reason at least one import of the module is required is that if the module isn't included in compilation at all, the global never gets declared. But once the global is declared it can be accessed from anywhere. |
@andy-ms but what if you want to extend some interface/class/namespace (from global scope or from some other module)? For example like in rxjs. Seems that |
@timocov That example is a module augmentation. It modifies a different module than the current one, so you're right that it's global in effect, i.e. you don't have to import the augmenter to see the effect in the augmented module. |
In the example of "Module Augmentation" augmenter is imported explicitly: // consumer.ts
import { Observable } from "./observable";
import "./map"; and I believe this is correct if we want do not have |
@andy-ms Consider this import clause: But the type system behavior is different. Compiler assumes that if the module was ever built (and the "declare global" or "declare module" statement in this module is parsed and applied during build), then the side effects are applied too, without any alignment with the module loading in runtime. So, we end up relying on side effects in all the modules even if we do not explicitly import the module producing that side effects. |
@Nipheris That's unfortunate, but the compiler doesn't always analyze the order things are executed in anyway: f();
function f() {
console.log(x);
}
const x = 0; This code will type-check but fail at runtime (assuming you target |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
TypeScript Version: 2.8.0-dev.20180314
Search Terms: global declarations, ambient context.
Code
a.ts
entry.ts
entry2.ts
tsconfig.json
Expected behavior:
Error in
entry2.ts
about undeclared member.Actual behavior:
No errors.
If we comment out the
import './a';
inentry.ts
, compilation fails with the following errors in BOTH modules (as expected):But if we import
a.ts
in the first entry file, both errors disappear, so, using the import in the first entry module produces side effects on the second one. This is a bit surprising thatdeclare global
produces such side effects even when source file is in "module" mode. It leads to the mismatch between declared types and the other side effects likewindow.someGlobalVar = 10
.The text was updated successfully, but these errors were encountered: