Skip to content

Proposal: Add global modifier to the language #4870

Closed
@iam3yal

Description

@iam3yal

Background

JavaScript has many global variables/functions, we tend to work with them all the time but sometimes we might not even know about them or use global names that can bite us later, at some point.

The more libraries we add to our projects the more globals we tend to use and sometimes it can get confusing, my last proposal was about adding warning to the language but really, after some thoughts and feedback, who wants to see many warnings issuing for using globals! it can be annoying and like I've stated we're using them all the time!

What I'm proposing now, is a modifier that will allow us to declare something as global, it will definitely help tools to differentiate between non-globals and globals and it will definitely help us to know about them.

Use cases

Libraries authors can finally be explicit about their globals which is tenfold better than having comments in the definition files mainly for readability and tools awareness as described below.

Readability: Consumers that sometimes read the definition files for APIs won't have to think twice whether something is global or not.

Awareness: Having a modifier for globals will allow tools to be aware of globals and so this can cover scenarios like syntax highlighting, refactoring, issuing warnings about certain globals such as when we try to reassign a new value to a global variables.

Maintainability: Due to the two points above it makes maintainability a lot easier to deal with especially in a large codebase where the amount of globals is inevitably bigger, so spotting them can be made crystal clear.

Syntax

Variables:

declare global var name: type

Functions:

declare global function name(): type

Examples

Here are few examples of how it will look like in practice.

The only difference between how things are declared now and how they will be declared later (if this feature will get implemented) is the addition of the global modifier as shown below.

lib.d.ts

declare global var RegExp: RegExpConstructor;
declare global var console: Console;

node.d.ts

declare global var process: NodeJS.Process;
declare global var global: NodeJS.Global;

declare global var __filename: string;
declare global var __dirname: string;

declare global function setTimeout(callback: (...args: any[]) => void, ms: number, ...args: any[]): NodeJS.Timer;

Clarification

I thought that it's clear from the examples above but the point of this proposal is to be explicit about what is global and what isn't by having a global modifier in the language as part of the declare statement, this will allow people to be more specific with their declare statements and then tools would have the ability to take advantage of that.

It has nothing to do with declaring global variables nor accessing them and because it's part of the declare statement nothing is ever emitted to JavaScript.

I hope it's crystal clear now. :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions