Skip to content

[Discussion] Better Way to Declare Globals in JavaScript files #15626

@mjbvz

Description

@mjbvz

Problem
Using the new checkJs feature, it is currently difficult to tell TypeScript about a global variable:

// @ts-check

myGlobal

window.myVar = 'foo';

screen shot 2017-05-05 at 4 44 20 pm

The only way around this that I know is to create a .d.ts file that defines these:

interface Window {
    myVar: string;
}

declare var myGlobal: string;

JavaScript users should not have to write *.d.ts files.

Possible Approaches
@egamma proposes that may be able to use the global comment from ESlint http://eslint.org/docs/user-guide/configuring : /* global var1, var2 */

I'd also be interested to see if we could put type information in a jsdoc somehow, something like:

/**
 * @global {string} myGlobal 
 */

Metadata

Metadata

Assignees

Labels

Domain: JavaScriptThe issue relates to JavaScript specificallyNeeds ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScriptVS Code TrackedThere is a VS Code equivalent to this issue

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions