Skip to content

[no-shadow] declaring a global causes no-shadow to error #2654

Closed
@salemhilal

Description

@salemhilal

Hey, I think I found what's either a bug or a confusing way that globals and ambient types are handled (potentially similarly to the no-undef issue mentioned in the FAQs).

I'm currently migrating a large codebase to use TypeScript. We have a global has() function that we use to determine what environment we're in. It's handled by our build system, and is set as a read-only global in our existing ESLint config.

When I try and declare an ambient type for this global, I've noticed that @typescript-eslint/no-shadow thinks that I'm shadowing it. Removing globals: { has: false } from the config fixes this problem. Is this expected behavior?

Thanks in advance, and sorry if I'm overlooking something.

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

I set up a fresh project by running the following in an empty directory:

yarn init -y
yarn add -D eslint typescript @typescript-eslint/parser @typescript-eslint/eslint-plugin

I then added these two files

// .eslintrc.js
module.exports = {
  root: true,
  parser: '@typescript-eslint/parser',
  plugins: [
    '@typescript-eslint',
  ],
  extends: [
    'eslint:recommended',
    'plugin:@typescript-eslint/recommended',
  ],
  globals: {
    has: false,
  },
  rules: {
    "no-shadow": "off",
    "@typescript-eslint/no-shadow": ["error", { builtinGlobals: true }],
  }
};
// index.d.ts
declare const has: (environment: "dev" | "prod" | "test") => boolean;

Expected Result

I'd expect yarn eslint index.d.ts to not error.

Actual Result

It logged the following error:

 1:15  error  'has' is already declared in the upper scope  @typescript-eslint/no-shadow

Versions

package version
@typescript-eslint/eslint-plugin 4.4.0
@typescript-eslint/parser 4.4.0
TypeScript 4.0.3
ESLint 7.10.0
node 12.15.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issueenhancementNew feature or requestgood first issueGood for newcomerslocked 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