Skip to content

Base rule extension: no-var configuration for declarations #7941

@DanKaplanSES

Description

@DanKaplanSES

Before You File a Proposal Please Confirm You Have Done The Following...

My proposal is suitable for this project

  • My proposal specifically checks TypeScript syntax, or it proposes a check that requires type information to be accurate.
  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Link to the base rule

https://eslint.org/docs/latest/rules/no-var

Description

Typescript global declarations are more subtle than I ever expected. For a better understanding, I recommend these SO answers:

Here are some key takeaways. If you want to add properties to globalThis:

  1. When the typescript file has no import or export keyword (i.e., it's a script, not a module), it is done like so: declare var age: number.
  2. When the typescript file does have import or export, you write this instead:
    // an import / export line
    
    declare global {
        var age: number;
    }

If const or let is used instead of var, globalThis.age = 18 would error in both scenarios. So if the intention is to declare globalThis.age, it is necessary to use the var keyword: using const or let would be a mistake. When enabled, the base no-var rule indiscriminately marks all var usage as wrong, but in this case, it is required.

Fail

/*eslint no-var: "error"*/
export {}

var x = "y";
var CONFIG = {};

Pass

export {}

declare global {
    var age: number;
}

Additional Info

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement: new base rule extensionNew base rule extension required to handle a TS specific caseexternalThis issue is with another package, not typescript-eslint itselflocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.package: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy