You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Definitions declaring a global var create collisions & global-shadow #1564
I see many definitions declare a global variable, with the name most commonly used. For example jQuery defines $, and many other do similar.
Problem: the namespace for short identifiers is not unlimited. If these are used a lot it can potentially make it impossible to mix modules. So if you have a half-migrated test suite using two different assertion libraries that both declare a global var assert.
Even worse: if you use a definition with a global var but due to the way you use it it never gets initialised (because run it in node.js or use an AMD loader), then you have a global var shadowing your scoped usage (as seen by the compiler).
That is not good if you accidentally use it without actually initialising it at runtime. Then what you think is a scoped var gets attached to the global/window object (unless you lint, but who lints compiler output?)
And of course: once you use a definition with a specific global var then you loose your own use of that identifier in your own global scope, even if you never need the global var (because you use import or whatever)
Not sure what to do with this.
It feels unclean 😷. Maybe the users should declare their own var for the few cases where they actually use a global?
What if TSD automatically generates global var declarations as needed, based on the user's chosen configuration in tsd.json?
In order for this to work, "well behaved" definitions should never expose globals; they always expose namespaced modules.
For example, suppose my project uses Knockout, LoDash, and jQuery. I load Knockout and LoDash using an AMD loader, so they do not export any global variables. However, I do allow jQuery to create window.$.
In my tsd.json I list those three dependencies, and also have this:
...
"globals": {
"jquery": "$"
}
...
Based on that configuration, tsd will create a globals.d.ts file that contains:
declare var $: typeof require('jquery');
// This syntax doesn't work, but demonstrates the idea
Alternatively, you could simplify the tsd.json configuration to be a boolean flag per module. Set "globals": {"jquery": true} and tsd does the rest. This would require the .d.ts file(s) to contain an optional declare var that tsd can selectively enable or disable. Or you could require definition authors to create global var declarations in a separate file, such as jquery-globals.d.ts?
At current count it looks like there are ~2400 global variables declared (gci -r -fi '*.d.ts' | grep '^declare var' | measure).
I really think something needs to be done about these, we just had issues with forgetting to import knockout into a module, but because knockout.d.ts declares a global ko the compiler was fine with it, resulting in a runtime crash when actually attempting to access ko.
Best fix would be something like mentioned above to have tsd do the work for you.
Otherwise, for a lot of these I feel like just removing the var is fine. It's very little work for users to add the var back in, it's much more work for those that don't want it to have a fork of the repo with a definition file without the var and keep merging updates to definitions into it.
For users that do want the var they simply need to add a local typings.d.ts (or whatever they want to call it) that references the one generated by tsd and adds in their extras.
The typings project addresses this issue. It allows external (npm-style) and ambient (global-style) modules, and you pick the type you want to use.
Also, Typescript now allows extending interfaces declared in other ambient modules (see "Module Augmentation" in this blog post) which eliminates the need for namespaces in global scope.
Activity
cspotcode commentedon Jan 23, 2014
What if TSD automatically generates global var declarations as needed, based on the user's chosen configuration in
tsd.json?In order for this to work, "well behaved" definitions should never expose globals; they always expose namespaced modules.
For example, suppose my project uses Knockout, LoDash, and jQuery. I load Knockout and LoDash using an AMD loader, so they do not export any global variables. However, I do allow jQuery to create
window.$.In my
tsd.jsonI list those three dependencies, and also have this:Based on that configuration, tsd will create a
globals.d.tsfile that contains:Alternatively, you could simplify the
tsd.jsonconfiguration to be a boolean flag per module. Set"globals": {"jquery": true}and tsd does the rest. This would require the .d.ts file(s) to contain an optionaldeclare varthat tsd can selectively enable or disable. Or you could require definition authors to create global var declarations in a separate file, such asjquery-globals.d.ts?Bartvds commentedon Jan 23, 2014
This is a good idea. I made a ticket in the tsd repo: DefinitelyTyped/tsd#55
We could also add a 'preferred global' field to the future manifest file here on DT.
Nemo157 commentedon Feb 22, 2015
1 year later, still not fixed 😞.
At current count it looks like there are ~2400 global variables declared (
gci -r -fi '*.d.ts' | grep '^declare var' | measure).I really think something needs to be done about these, we just had issues with forgetting to import knockout into a module, but because knockout.d.ts declares a global
kothe compiler was fine with it, resulting in a runtime crash when actually attempting to accessko.Best fix would be something like mentioned above to have
tsddo the work for you.Otherwise, for a lot of these I feel like just removing the
varis fine. It's very little work for users to add thevarback in, it's much more work for those that don't want it to have a fork of the repo with a definition file without thevarand keep merging updates to definitions into it.For users that do want the
varthey simply need to add a localtypings.d.ts(or whatever they want to call it) that references the one generated bytsdand adds in their extras.slaneyrw commentedon Jun 1, 2016
2 years later and this is still biting us in development
cspotcode commentedon Jun 1, 2016
The
typingsproject addresses this issue. It allows external (npm-style) and ambient (global-style) modules, and you pick the type you want to use.Also, Typescript now allows extending interfaces declared in other ambient modules (see "Module Augmentation" in this blog post) which eliminates the need for namespaces in global scope.
ffMathy commentedon Nov 11, 2019
@Nemo157 it's probably even worse, since there's also
declare constanddeclare let😏