Skip to content

Definitions declaring a global var create collisions & global-shadow #1564

Open

Description

@Bartvds

From another thread: #1562 (comment)

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 do you think?

Activity

  1. cspotcode commented on Jan 23, 2014

    @cspotcode
    Contributor

    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?

  2. Bartvds commented on Jan 23, 2014

    @Bartvds
    CollaboratorAuthor

    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.

  3. Nemo157 commented on Feb 22, 2015

    @Nemo157
    Contributor

    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 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.

    /// <reference path="typings/tsd.d.ts" />
    
    declare var $: JQueryStatic;
    declare var ko: KnockoutStatic;
  4. slaneyrw commented on Jun 1, 2016

    @slaneyrw

    2 years later and this is still biting us in development

  5. cspotcode commented on Jun 1, 2016

    @cspotcode
    Contributor

    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.

  6. ffMathy commented on Nov 11, 2019

    @ffMathy
    Contributor

    @Nemo157 it's probably even worse, since there's also declare const and declare let 😏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions